if we have a table T1 with columns A,B,C,D,E and an index ( A,B,C) built for it
if we a SQL query joining on columns A,B or A,B,C or A, this index can still be used, but if the query is joining on B or C or B,C the index is totally useless
I know indexes are often implemented BTree , I want to know how is the implementation detail related with this ?
-
All you ever wanted to know about indexes: use-the-index-luke.comuser1822– user18222014年01月15日 07:29:09 +00:00Commented Jan 15, 2014 at 7:29
-
use-the-index-luke.com/sql/where-clause this is indeed a very good bookzinking– zinking2014年01月17日 01:27:58 +00:00Commented Jan 17, 2014 at 1:27
-
use-the-index-luke.com/sql/where-clause/the-equals-operator/… this is the detailed explaination on this topic.zinking– zinking2014年01月17日 06:49:05 +00:00Commented Jan 17, 2014 at 6:49
1 Answer 1
This is not necessarily the case. Oracle, for example, has an access path known as an "index skip scan". See http://docs.oracle.com/cd/B10501_01/server.920/a96533/optimops.htm#51553
Basically, if there are few distinct values in column A, and the query restricts on column B (and optionally, column C), the executor will substitute each of the distinct values of column A in turn and probe the index for the supplied value of column B (and optionally, column C).
Postgresql can do something similar, but it is still labelled as an "index scan", see Working of indexes in PostgreSQL
-
1There is a Connect item that one can vote for implementing this in SQL-Server: MS Connect: Implement Index Skip Scanypercubeᵀᴹ– ypercubeᵀᴹ2014年01月15日 10:11:54 +00:00Commented Jan 15, 2014 at 10:11
-
I think this states that my assertion is not necessarily true. but without considering this, how does DB apply INDEX ABC on column AB join ? is that by comparing only the AB part of the index key ?zinking– zinking2014年01月16日 08:39:13 +00:00Commented Jan 16, 2014 at 8:39