Connecting Tech Pros Worldwide Forums | Help | Site Map

index usage when using SUBSTR

crazy_jutt
Guest
 
Posts: n/a
#1: Dec 15 '06
hi all,

i heard that db2 ignores indexes when using any column function on the
column which has index on it. but i have seen db2 using indexes even
when using column function.

what is the criteria of db2 about picking index or not picking index
when there is a column function ?

i am using function on one column like below

SUBSTR(abc,1,1) IN ('SS',V'V',OO',PP',EE') and db2 is doing table scan
and Filter Factor : 0.0798775

what is the best way to deal with this kind of criteria in where clause
?
should there be extra column added to table with only SUBSTR data from
original column and make index on that column and then use that column
in where instead


Mark A
Guest
 
Posts: n/a
#2: Dec 15 '06

re: index usage when using SUBSTR


"crazy_jutt" <udbadmin@gmail.comwrote in message
news:1166211368.107283.20220@t46g2000cwa.googlegro ups.com...
Quote:
hi all,
>
i heard that db2 ignores indexes when using any column function on the
column which has index on it. but i have seen db2 using indexes even
when using column function.
>
what is the criteria of db2 about picking index or not picking index
when there is a column function ?
>
i am using function on one column like below
>
SUBSTR(abc,1,1) IN ('SS',V'V',OO',PP',EE') and db2 is doing table scan
and Filter Factor : 0.0798775
>
what is the best way to deal with this kind of criteria in where clause
?
should there be extra column added to table with only SUBSTR data from
original column and make index on that column and then use that column
in where instead
I believe that you meant WHERE SUBSTR(abc,1,2) IN ('SS',VV','OO',PP',EE')
since the literals have 2 bytes.

In this particular case, you might try:
WHERE abc like 'SS%'
or abc like 'VV%',
or abc like 'OO%',
or abc like 'PP%',
or abc like 'EE%'

Note that there are 2 main types of index access. One is the b-tree (which
is the fastest and what most people think of as index access), and there is
also a index space scan where it reads all the leaf pages of the index
without the b-tree (non-leaf pages). The later is slower, but faster than a
table scan.

A statement with "WHERE abc like 'SS%' " will normally use the b-tree of the
index since the leftmost characters are being searched (there is no leading
%). I am not quite sure about what will happen with all the OR's.


Tonkuma
Guest
 
Posts: n/a
#3: Dec 16 '06

re: index usage when using SUBSTR


I tested by using Sample table data as following.
The last query used the Index.
Has your index include only column abc?
Or, do you have many AND conditions and is another index more
effective?
If your FixPack of DB2 is low, it may be better to upgrade latest FP.

------------------------------ Commands Entered
------------------------------
CREATE TABLE Employee_I LIKE Employee;
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------------ Commands Entered
------------------------------
INSERT INTO Employee_I SELECT * FROM Employee;
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------------ Commands Entered
------------------------------
CREATE INDEX EmpI_fnm ON Employee_I
(firstnme);
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.

------------------------------ Commands Entered
------------------------------
SELECT empno, lastname || ', ' || firstnme AS "FullName"
FROM Employee_I
WHERE SUBSTR(firstnme,1,2) IN ('SA','MA','IA','VI','WI');
------------------------------------------------------------------------------

EMPNO FullName
------ -----------------------------
000270 PEREZ, MARIA
000180 SCOUTTEN, MARILYN
000170 YOSHIMURA, MASATOSHI
000310 SETRIGHT, MAUDE
000030 KWAN, SALLY
000240 MARINO, SALVATORE
000110 LUCCHESSI, VINCENZO
000210 JONES, WILLIAM
000330 LEE, WING

9 record(s) selected.

Tonkuma
Guest
 
Posts: n/a
#4: Dec 16 '06

re: index usage when using SUBSTR



Tonkuma wrote:
Quote:
I tested by using Sample table data as following.
The last query used the Index.
Has your index include only column abc?
Or, do you have many AND conditions and is another index more
effective?
I'm sorry. I made mistake. You wrote DB2 do tablespace scan.

crazy_jutt
Guest
 
Posts: n/a
#5: Dec 19 '06

re: index usage when using SUBSTR


hi,

first, thanks to all trying to help
using LIKE clause with OR made explain plain go crazy with 10 to power
38 timerons from 798000 timerons withSUBSTR (abc,1,1) IN
('SS',V'V',OO',PP',EE')
so, i stayed with SUBSTR(abc,1,1) IN ('SS',V'V',OO',PP',EE')
there were three more columns along with abc used in the same where
clause from this table say XYZ
i created compund index with all four columns and ordered column in
index with decreasing cardinality and allow reverse scans
now, it used index for SUBSTR(abc,1,1) IN ('SS',V'V',OO',PP',EE')
i do not understand this behaviour. previously, i had indexes on
individual columns and db2 did use indexes for all columns from table
XYZ but abc column
not when i create compound index, db2 uses index for all.
please someone explain

also, give me some good web,book,articles references to understand
behaviour of indexes in db2




On Dec 15, 10:51 pm, "Tonkuma" <tonk...@jp.ibm.comwrote:
Quote:
Tonkuma wrote:
Quote:
I tested by using Sample table data as following.
The last query used the Index.
Has your index include only column abc?
Or, do you have many AND conditions and is another index more
effective?I'm sorry. I made mistake. You wrote DB2 do tablespace scan.
Closed Thread