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 > Compound index in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-07, 12:52
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Compound index in DB2

Hi,
I am trouble shooting a performance problem.
I run a similar query against two different tables, both have a compound primary key and in both this compound key is indexed.
The columns that are the compound primary key and the index are:
ClassId and Index.

The 1st Query is

SELECT CLASSID, INDEX, HEIGHT, WEIGHT, AGE FROM PERSON
WHERE CLASSID = 5 ORDER BY CLASSID, INDEX;

The 2nd Query is against EMPLOYEE and is

SELECT CLASSID, INDEX, HEIGHT, WEIGHT, AGE FROM EMPLOYEE
WHERE CLASSID = 6 ORDER BY CLASSID, INDEX;;

When I look at the explain plans, one query uses the index the other does not.
My question is if a index is a combination of columns, when is that index actually used?
Is it when
-both columns are specified in the where clause?
-both columns are specified in the orderBy clause?
-when one of the two columns is specifed in the where clause?
-when one of the two columns is specifed in the order by clause?
or
-all the time as it is also the primary key?

Any help greatly appreciated!

Last edited by Breako; 01-08-07 at 12:57.
Reply With Quote
  #2 (permalink)  
Old 01-08-07, 14:15
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Neither of these statements is correct. The right way to put it would be "An index, whether compound or not, is used when the optimizer thinks it is beneficial".
Reply With Quote
  #3 (permalink)  
Old 01-08-07, 16:18
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
There are 2 kinds of index usage:

1. B-tree access using the non-leaf pages to quickly locate the leaf pages with the corresponding RID's. This is what most people think of when they say "index access".

2. Scanning all the leaf pages without any access to the non-leaf pages. This is like a tablespace scan of the the entire index.

To use access type number 1 above with a composite index, then at least the left most column(s) of the multi-column index must be supplied in the predicate (WHERE Clause). Otherwise, DB2 will not use the index, or may use access type 2 above.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 01-09-07, 06:20
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Quote:
Originally Posted by Marcus_A
There are 2 kinds of index usage:

1. B-tree access using the non-leaf pages to quickly locate the leaf pages with the corresponding RID's. This is what most people think of when they say "index access".

2. Scanning all the leaf pages without any access to the non-leaf pages. This is like a tablespace scan of the the entire index.

To use access type number 1 above with a composite index, then at least the left most column(s) of the multi-column index must be supplied in the predicate (WHERE Clause). Otherwise, DB2 will not use the index, or may use access type 2 above.
Thanks for your replies.
It appears that if the table has no data an index scan is used, but if the table has data an index scan is not used. Any comments appreciated.
Reply With Quote
  #5 (permalink)  
Old 01-09-07, 10:06
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
DB2 will usually do a table scan if the number of rows is relatively small, because it is faster than using an index. This assumes runstats has been run and DB2 knows the number of rows in the table. A new table may have stats set to -1, which means that runstats has not been run.

If you alter the table to volatile, then DB2 will ignore the stats and is more likely to use an index if a suitable index is available.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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