473,769 Members | 5,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query doing table scan when added simple predicates

Hi
I was using a query previously, that was efficient
select * from table where pred1 and pred2 and pred3;

Later I was asked to introduce new ones, but they were not based on
table columns but variables declared in SP.

select * from table where pred1 and pred2 and pred3 and variable1
='number1 and variable2 =number2;

so in turn this translates to pure mathematical comparison, sometimes
simple like 2=2,
I;ve added only 2 of them in the ex above, but actually there are 13
(does this influence the path, duh!!). Anyways, what happened was,
after they were added, it started doing a table scan, and was very
very inefficient. Later, I converted the additional predicates into
simple if loops, and executed the original query, and SP was very
fast.
Is there any database logic connected to doing a table scan? or any
registry/variable setting I can do so db2 doesnt do the table scan?
Please advise, since my team has done this mistake in many places,
and rolling back is going to be a pain in backside.
Nov 13 '08 #1
4 3885
Arun Srinivasan wrote:
Hi
I was using a query previously, that was efficient
select * from table where pred1 and pred2 and pred3;

Later I was asked to introduce new ones, but they were not based on
table columns but variables declared in SP.

select * from table where pred1 and pred2 and pred3 and variable1
='number1 and variable2 =number2;

so in turn this translates to pure mathematical comparison, sometimes
simple like 2=2,
I;ve added only 2 of them in the ex above, but actually there are 13
(does this influence the path, duh!!). Anyways, what happened was,
after they were added, it started doing a table scan, and was very
very inefficient. Later, I converted the additional predicates into
simple if loops, and executed the original query, and SP was very
fast.
Is there any database logic connected to doing a table scan? or any
registry/variable setting I can do so db2 doesnt do the table scan?
Please advise, since my team has done this mistake in many places,
and rolling back is going to be a pain in backside.
Which version and platform are you on?
New versions of Db2 for LUW typically "push out" "constant predicates".
That is:
SELECT * FROM T WHERE pred(T) AND pred(context)
is turned into:
SELECT * FROM (SELECT 1 FROM VALUES(1) WHERE pred(context)), (SELECT *
FROM T WHERE pred(T))
with the constant pred being the outer of a nested loop join.
You should be able to see this in a db2exfmt output

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Nov 13 '08 #2
I am really surprised at the following explains.

1. The correct one, with just one constant predicate check.

select ri_indv_id

from elg.artelgf

where client_cd ='300' and segment_type_cd ='003' and carrier_cd =

'60054' and ri_indv_id =11233 and covpf_rf ='MED' and
--- below is the only constant predicate check
value(

'01-01-2009' , CURRENT DATE)CURRENT DATE - 30 days

fetch first 1 rows only

Section Code Page = 1252

Estimated Cost = 38.574055

Estimated Cardinality = 0.047454

( 3) Table Constructor

| 1-Row(s)

( 3) Residual Predicate(s)

| #Predicates = 1

( 2) Nested Loop Join

( 2) | Access Table Name = ELG.ARTELGF ID = 47,4

| | Index Scan: Name = ELG.PK_ARTELGF2 ID = 6

| | | Regular Index (Not Clustered)

| | | Index Columns:

| | | | 1: CLIENT_CD (Ascending)

| | | | 2: SEGMENT_TYPE_CD (Ascending)

| | | | 3: CARRIER_CD (Ascending)

| | | | 4: RI_INDV_ID (Ascending)

| | | | 5: COVPF_RF (Ascending)

| | | | 6: GROUP_NUM (Ascending)

| | | | 7: POLICY_NUM (Ascending)

| | | | 8: POLICY_START_DT (Ascending)

| | | | 9: CARRIER_OFFICE_ CD (Ascending)

| | | | 10: SGSRC_RF (Ascending)

| | | | 11: QHIT_IND (Include Column)

| | #Columns = 0

| | Compressed Table

| | Skip Inserted Rows

| | Skip Deleted Keys

| | Skip Deleted Rows

| | #Key Columns = 5

| | | Start Key: Inclusive Value

| | | | | 1: '300 '

| | | | | 2: '003'

| | | | | 3: '60054'

| | | | | 4: 000000011233

| | | | | 5: 'MED'

| | | Stop Key: Inclusive Value

| | | | | 1: '300 '

| | | | | 2: '003'

| | | | | 3: '60054'

| | | | | 4: 000000011233

| | | | | 5: 'MED'

| | Index-Only Access

| | Index Prefetch: None

| | Lock Intents

| | | Table: Intent Share

| | | Row : Next Key Share

( 1) Return Data to Application

| #Columns = 1

End of section

Optimizer Plan:

RETURN

( 1)

|

NLJOIN

( 2)

/ \

TBSCAN IXSCAN

( 3) ( 2)

| / \

TFunc: Index: Table:

SYSIBM ELG ELG

GENROW PK_ARTELGF2 ARTELGF

2. Here we have the same query but havin 2 const predicate checks
instead of one and see what our db2 engine comes up with.
select ri_indv_id

from elg.artelgf

where client_cd ='300' and segment_type_cd ='003' and carrier_cd =

'60054' and ri_indv_id =11233 and covpf_rf ='MED' and
--below are two const predicate checks instead of 1
value(

'01-01-2009' , CURRENT DATE)CURRENT DATE - 30 days and ( (

'01-01-1999' between '01-01-2000' and '01-01-2009' AND days(

value('01-01-2009' , current date))- days('01-01-1999' )>=

30 ))

fetch first 1 rows only

Section Code Page = 1252

Estimated Cost = 0.000101

Estimated Cardinality = 0.000000

( 2) Table Constructor

| N-Rows

( 2) Residual Predicate(s)

| #Predicates = 1

( 1) Return Data to Application

| #Columns = 1

End of section

Optimizer Plan:

RETURN

( 1)

|

TBSCAN

( 2)

|

TFunc:

SYSIBM

GENROW

Nov 14 '08 #3
On Nov 14, 2:17*pm, Arun Srinivasan <arunro...@gmai l.comwrote:
I am really surprised at the following explains.

1. The correct one, with just one constant predicate check.

select ri_indv_id

* from elg.artelgf

* where client_cd ='300' and segment_type_cd ='003' and carrier_cd =

* * * * * '60054' and ri_indv_id =11233 and covpf_rf ='MED'and
--- below is the only constant predicate check
* * * * * value(

* * * * * '01-01-2009' , CURRENT DATE)CURRENT DATE - 30 days

* fetch first 1 rows only

Section Code Page = 1252

Estimated Cost = 38.574055

Estimated Cardinality = 0.047454

( * *3) Table Constructor

* * * * | *1-Row(s)

( * *3) Residual Predicate(s)

* * * * | *#Predicates = 1

( * *2) Nested Loop Join

( * *2) | *Access Table Name = ELG.ARTELGF *ID = 47,4

* * * * | *| *Index Scan: *Name = ELG.PK_ARTELGF2 *ID = 6

* * * * | *| *| *Regular Index (Not Clustered)

* * * * | *| *| *Index Columns:

* * * * | *| *| *| *1: CLIENT_CD (Ascending)

* * * * | *| *| *| *2: SEGMENT_TYPE_CD (Ascending)

* * * * | *| *| *| *3: CARRIER_CD (Ascending)

* * * * | *| *| *| *4: RI_INDV_ID (Ascending)

* * * * | *| *| *| *5: COVPF_RF (Ascending)

* * * * | *| *| *| *6: GROUP_NUM (Ascending)

* * * * | *| *| *| *7: POLICY_NUM (Ascending)

* * * * | *| *| *| *8: POLICY_START_DT (Ascending)

* * * * | *| *| *| *9: CARRIER_OFFICE_ CD (Ascending)

* * * * | *| *| *| *10: SGSRC_RF (Ascending)

* * * * | *| *| *| *11: QHIT_IND (Include Column)

* * * * | *| *#Columns = 0

* * * * | *| *Compressed Table

* * * * | *| *Skip Inserted Rows

* * * * | *| *Skip Deleted Keys

* * * * | *| *Skip Deleted Rows

* * * * | *| *#Key Columns = 5

* * * * | *| *| *Start Key: Inclusive Value

* * * * | *| *| *| *| *1: '300 *'

* * * * | *| *| *| *| *2: '003'

* * * * | *| *| *| *| *3: '60054'

* * * * | *| *| *| *| *4: 000000011233

* * * * | *| *| *| *| *5: 'MED'

* * * * | *| *| *Stop Key: Inclusive Value

* * * * | *| *| *| *| *1: '300 *'

* * * * | *| *| *| *| *2: '003'

* * * * | *| *| *| *| *3: '60054'

* * * * | *| *| *| *| *4: 000000011233

* * * * | *| *| *| *| *5: 'MED'

* * * * | *| *Index-Only Access

* * * * | *| *Index Prefetch: None

* * * * | *| *Lock Intents

* * * * | *| *| *Table: Intent Share

* * * * | *| *| *Row *: Next Key Share

( * *1) Return Data to Application

* * * * | *#Columns = 1

End of section

Optimizer Plan:

* * * * *RETURN

* * * * *( * 1)

* * * * * *|

* * * * *NLJOIN

* * * * *( * 2)

* * * * / * * *\

*TBSCAN * * * * IXSCAN

*( * 3) * * * * ( * 2)

* *| * * * * * / * * *\

*TFunc: *Index: * * * Table:

*SYSIBM *ELG * * * * *ELG

*GENROW *PK_ARTELGF2 *ARTELGF

2. Here we have the same query but havin 2 const predicate checks
instead of one and see what our db2 engine comes up with.
*select ri_indv_id

* from elg.artelgf

* where client_cd ='300' and segment_type_cd ='003' and carrier_cd =

* * * * * '60054' and ri_indv_id =11233 and covpf_rf ='MED'and
--below are two const predicate checks instead of 1
*value(

* * * * * '01-01-2009' , CURRENT DATE)CURRENT DATE - 30 days and ( (

* * * * * '01-01-1999' between '01-01-2000' and '01-01-2009' AND days(

* * * * * value('01-01-2009' , current date))- days('01-01-1999' )>=

* * * * * 30 ))

* fetch first 1 rows only

Section Code Page = 1252

Estimated Cost = 0.000101

Estimated Cardinality = 0.000000

( * *2) Table Constructor

* * * * | *N-Rows

( * *2) Residual Predicate(s)

* * * * | *#Predicates = 1

( * *1) Return Data to Application

* * * * | *#Columns = 1

End of section

Optimizer Plan:

*RETURN

*( * 1)

* *|

*TBSCAN

*( * 2)

* *|

*TFunc:

*SYSIBM

*GENROW
I got it, actually db2 is far intelligent than what I thought :-p the
reason for this behaviour being, since one of the
constant predicates check was false, it didnt even go to the real
query.
Hail DB2,
Nov 14 '08 #4
Arun Srinivasan wrote:
>Optimizer Plan:

RETURN

( 1)

|

NLJOIN

( 2)

/ \

TBSCAN IXSCAN

( 3) ( 2)

| / \

TFunc: Index: Table:

SYSIBM ELG ELG

GENROW PK_ARTELGF2 ARTELGF
That's the push out I described (GENROW == VALUES).
>Optimizer Plan:

RETURN

( 1)

|

TBSCAN

( 2)

|

TFunc:

SYSIBM

GENROW

I got it, actually db2 is far intelligent than what I thought :-p the
reason for this behaviour being, since one of the
constant predicates check was false, it didnt even go to the real
query.
Hail DB2,
Indeed.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Nov 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2659
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I need to be able to do a search for specific tickets withing price ranges, different locations within the theaters, etc. etc. My problem is in the search one of the criteria is to search for a group of seats together. For example let's say...
4
2863
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table that links to a transaction items table that links to the products table: (User Table) UserID Other user data
7
10815
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL , "Y" REAL NOT NULL , "Z" REAL NOT NULL )
5
6340
by: sql-db2-dba | last post by:
We have DB2 UDB v8.1 fixpak3 on AIX 5. Production and Development configuarations (at least for DB2) are identical albeit production is a 2-way server while development has only one processor. Tables and indexes have the same schema. In fact, the dev database was taken from a prod backup recently. Size of the tables differ slightly. Yet, on a given query (with 4 tables joined), it took 30-50 times longer to run in prod than on development....
4
2085
by: JayCallas | last post by:
I have a SQL 2000 table containing 2 million rows of Trade data. Here are some of the columns: INT IDENTITY(1,1) -- PK, non-clustered DATETIME -- clustered index DATETIME -- non-clustered index VARCHAR(10) VARCHAR(10) INT etc..
10
2205
by: Raj | last post by:
I have an MDC index on Big_A.Dt column. The following query always goes for a table scan. SELECT Key, Cd, Dt, SUM(Big_A ) FROM ( SELECT Big_A.Key , small_3.Cd,
6
2766
by: Hemant Shah | last post by:
Folks, I am having trouble with a query. DB2 does not use index, it does relation scan of the table. I am running DB2 UDB 8.2 on Fedora Core release 4 (Stentz) # db2level DB21085I Instance "dbap68" uses "32" bits and DB2 code release "SQL08015" with level identifier "02060106".
5
3846
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
3
2314
by: bob laughland | last post by:
Hi All, I have a SQL query like this (I have tried to break the problem down to simplify it), select rowid from table where name in ('a', 'b', 'd') group by rowid Here is an example of data in the table rowid name
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.