dbTalk Databases Forums  

Why isn't index used?

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Why isn't index used? in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mike L. Bell
 
Posts: n/a

Default Why isn't index used? - 08-15-2003 , 02:59 PM






I have a query that joins two tables. The optimizer plan looks like
this:

RETURN
( 1)
Quote:
GRPBY
( 2)
Quote:
TBSCAN
( 3)
Quote:
SORT
( 4)
Quote:
MSJOIN
( 5)
/ \
TBSCAN TBSCAN
( 6) ( 10)
Quote:
|
SORT SORT
( 7) ( 11)
Quote:
|
TBSCAN TBSCAN
( 8) ( 12)
Quote:
|
Table: Table:
DB2ADMIN DB2ADMIN
SCL_ITEM SCL


The primary keys for the two tables are:
SCL_ITEM SCL
------------ ------------
SCL_ID SCL_ID
PAGE_NBR PAGE_NBR
ITEM_ID


The query is:
select scl.scl_id, scl.page_nbr, count(*), sum(item.qty)
from scl scl join scl_item item
on scl.scl_id = item.scl_id
and scl.page_nbr = item.page_nbr
group by scl.scl_id, scl.page_nbr

The statistics are up to date, but this query runs slowly. I always
thought that the optimizer would choose to use an index path for a
join such as this, but it seems to prefer the table scan. Is there
another trick that I could try out to convince the optimizer to use
the index path? (Platform is AIX 4.3.3 UDB 7.2 fp5)

Thanks,
Mike


Reply With Quote
  #2  
Old   
Powell
 
Posts: n/a

Default Re: Why isn't index used? - 08-16-2003 , 11:08 AM






Mike, How about,

select scl.scl_id, scl.page_nbr, count(scl.scl_id), sum(item.qty)
from scl scl join scl_item item
on scl.scl_id = item.scl_id and
scl.page_nbr = item.page_nbr
group by scl.scl_id, scl.page_nbr


Powell.



mikelbell2000 (AT) yahoo (DOT) com (Mike L. Bell) wrote in message news:<49d7474a.0308151159.7457baaf (AT) posting (DOT) google.com>...
Quote:
I have a query that joins two tables. The optimizer plan looks like
this:

RETURN
( 1)
|
GRPBY
( 2)
|
TBSCAN
( 3)
|
SORT
( 4)
|
MSJOIN
( 5)
/ \
TBSCAN TBSCAN
( 6) ( 10)
| |
SORT SORT
( 7) ( 11)
| |
TBSCAN TBSCAN
( 8) ( 12)
| |
Table: Table:
DB2ADMIN DB2ADMIN
SCL_ITEM SCL


The primary keys for the two tables are:
SCL_ITEM SCL
------------ ------------
SCL_ID SCL_ID
PAGE_NBR PAGE_NBR
ITEM_ID


The query is:
select scl.scl_id, scl.page_nbr, count(*), sum(item.qty)
from scl scl join scl_item item
on scl.scl_id = item.scl_id
and scl.page_nbr = item.page_nbr
group by scl.scl_id, scl.page_nbr

The statistics are up to date, but this query runs slowly. I always
thought that the optimizer would choose to use an index path for a
join such as this, but it seems to prefer the table scan. Is there
another trick that I could try out to convince the optimizer to use
the index path? (Platform is AIX 4.3.3 UDB 7.2 fp5)

Thanks,
Mike

Reply With Quote
  #3  
Old   
AK
 
Posts: n/a

Default Re: Why isn't index used? - 08-17-2003 , 07:28 PM



you could use OPTIMIZE FOR xx ROWS clause.
As a result, most probably the query would run slower.

reorganizing both tables on the indexes on SCL_ID would speed the
query up and maybe eliminate slow sorts

you could also try to add all the involved columns to the indexes to
use index only access

Reply With Quote
  #4  
Old   
Matthew Emmerton
 
Posts: n/a

Default Re: Why isn't index used? - 08-17-2003 , 09:20 PM



"Mike L. Bell" <mikelbell2000 (AT) yahoo (DOT) com> wrote

Quote:
I have a query that joins two tables. The optimizer plan looks like
this:

RETURN
( 1)
|
GRPBY
( 2)
|
TBSCAN
( 3)
|
SORT
( 4)
|
MSJOIN
( 5)
/ \
TBSCAN TBSCAN
( 6) ( 10)
| |
SORT SORT
( 7) ( 11)
| |
TBSCAN TBSCAN
( 8) ( 12)
| |
Table: Table:
DB2ADMIN DB2ADMIN
SCL_ITEM SCL


The primary keys for the two tables are:
SCL_ITEM SCL
------------ ------------
SCL_ID SCL_ID
PAGE_NBR PAGE_NBR
ITEM_ID


The query is:
select scl.scl_id, scl.page_nbr, count(*), sum(item.qty)
from scl scl join scl_item item
on scl.scl_id = item.scl_id
and scl.page_nbr = item.page_nbr
group by scl.scl_id, scl.page_nbr

The statistics are up to date, but this query runs slowly. I always
thought that the optimizer would choose to use an index path for a
join such as this, but it seems to prefer the table scan. Is there
another trick that I could try out to convince the optimizer to use
the index path? (Platform is AIX 4.3.3 UDB 7.2 fp5)
The problem is that the index on SCL_ITEM is not useful for this query.
Yes, it contains the two columns used in the join, but it also contains a
third column (ITEM_ID) which is _not_ used in the query and thus DB2 ignores
the index.

If you create an index on SCL_ITEM that contains just the SCL_ID and
PAGE_NBR columns, then DB2 will use an index scan on both tables.

--
Matt Emmerton




Reply With Quote
  #5  
Old   
Tokunaga T.
 
Posts: n/a

Default Re: Why isn't index used? - 08-18-2003 , 01:11 AM



mikelbell2000 (AT) yahoo (DOT) com (Mike L. Bell) wrote in message
Quote:
The primary keys for the two tables are:
SCL_ITEM SCL
------------ ------------
SCL_ID SCL_ID
PAGE_NBR PAGE_NBR
ITEM_ID


The query is:
select scl.scl_id, scl.page_nbr, count(*), sum(item.qty)
from scl scl join scl_item item
on scl.scl_id = item.scl_id
and scl.page_nbr = item.page_nbr
group by scl.scl_id, scl.page_nbr

Do you need to join scl?
If (SCL_ID, PAGE_NBR) in SCL_ITEM table is a foreign key referencing
SCL table,
it will be not neccesary to join scl table.
Like this:
select item.scl_id, item.page_nbr, count(*), sum(item.qty)
from scl_item item
group by item.scl_id, item.page_nbr

Anyway, optimizer may not choose index access same as your original
query, because of efficiency of table scan only as Philip wrote.


Reply With Quote
  #6  
Old   
PM \(pm3iinc-nospam\)
 
Posts: n/a

Default Re: Why isn't index used? - 08-18-2003 , 08:00 PM



Not a good idea to hide the real predicates to us.
It's just like going to the garage and giving car-a for repair when it's
actually car-b that's broken.

Clustering index/table may help for the sort. (check also sort
parms/snapshots)
Your (!!**[--]Hidden[--]**!!) predicates are probably not part of the
index(es).


PM



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.