If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to enforce DB2 to use index scan instread of table scan?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-15-06, 03:18
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to enforce DB2 to use index scan instread of table scan?

Hi,

The following SQL:
Code:
SELECT A.DependentCol1, B.ParentCol1 FROM
DependentTable A LEFT OUTER JOIN ParentTable B
ON (A.DependentColFK = B.ParentColPK)
after reorg and runstats the above SQL uses 'table scan' (seeing from explain).

But adding a condition "AND B.ParentCol = COALESCE(B.ParentCol, B.ParentCol)" makes DB2 uses the 'index scan' which is in my case 3-times faster. Index is on column B.ParentColPK - this column is PK of ParentTable.

Target SQL looks like this:
Code:
SELECT A.DependentCol1, B.ParentCol1 FROM
DependentTable A LEFT OUTER JOIN ParentTable B
ON (A.DependentColFK = B.ParentColPK)
AND B.ParentCol = COALESCE(B.ParentCol, B.ParentCol)
Are there any general rules to enforce 'index scan' instread of 'table scan'? I am just looking is some general tips...

Thanks,
Grofaty
Reply With Quote
  #2 (permalink)  
Old 03-16-06, 09:52
jthakrar jthakrar is offline
Registered User
 
Join Date: Mar 2004
Posts: 46
There is no direct way of "enforcing the optimizer" (well its evolving in DB2) like in some of the other RDBMSes (e.g Oracle).

What you can do is enable the "volatile" property of the table as
ALTER TABLE X VOLATILE

The volatile property tells the optimizer to prefer index scan or table scan.
Read up the documentation and see if it helps.

If you are on DB2 v8.2 or above (8.1 fixpack 7 or above), then you can try to use the DB2 Advisor - it may shed some light on what will help your query.

Jayesh
Reply With Quote
  #3 (permalink)  
Old 03-17-06, 09:36
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Have you looked into using an optimization hint?
Reply With Quote
  #4 (permalink)  
Old 03-17-06, 14:58
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
What if you use an inner join from the parent to the child instead of an outer join from the child to the parent?

ex:
SELECT A.DependentCol1, B.ParentCol1 FROM
ParentTable B INNER JOIN DependentTable A
ON (A.DependentColFK = B.ParentColPK)

It should be functionally equivalent, assuming there is a true parent-child relationship between the tables. The optimizer might be smarter about it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On