472,809 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

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 3826
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...@gmail.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
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...
4
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...
7
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 ,...
5
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....
4
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 ...
10
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
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 ...
5
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...
3
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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.