Thread: Basic question on SQL indexes
07-09-09, 20:08 #1Registered User
- Join Date
- Mar 2006
Unanswered: Basic question on SQL indexes
I have a table xxx with cols c1, c2,c3,c4 in schema schema1
ihave created index for table xxx as
CREATE INDEX "SCHEMA1 "."IDX907092354290000" ON "SCHEMA1 "."XXX" ("C1" ASC, "C2" DESC, "C3" ASC) ALLOW REVERSE SCANS ;
which of the the sql query will use the index
SELECT C4 FROM SCHEMA1.XXX WHERE C1="A"
SELECT C4 FROM SCHEMA1.XXX WHERE C2="B" AND C3="C"
SELECT C4 FROM SCHEMA1.XXX WHERE C3="C"
07-09-09, 22:19 #2Registered User
- Join Date
- May 2003
Only the first query will use the b-tree of the index, which is by far the most efficient access path (assuming it is not a small table).
The other queries "may" or "may not" use the index, but if they use it, DB2 will have to read the entire index, sort of like a table scan on the index. The reason for that is that the first column of the index is not in the predicate.
You can google "b-tree index" to find out what that means if you don't understand.M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390