By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,456 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,456 IT Pros & Developers. It's quick & easy.

index usage when using SUBSTR

P: n/a
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

Dec 15 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"crazy_jutt" <ud******@gmail.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
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.
Dec 15 '06 #2

P: n/a
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.

Dec 16 '06 #3

P: n/a

Tonkuma wrote:
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.

Dec 16 '06 #4

P: n/a
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:
Tonkuma wrote:
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.
Dec 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.