Subject | Re: [firebird-support] using indices |
---|---|
Author | Dean Harding |
Post date | 2008-09-19T06:31:50Z |
Sergio H. Gonzalez wrote:
think of an index is as a pre-sorted list of pointers to the actual rows
(sorted by the columns of the index).
Let's do a quick example. Say you've got two columns like this:
Row# COLA COLB
1 A G
2 A B
3 B F
4 B A
5 C D
6 A C
7 B E
8 C H
And a separate index on each, the indexes will be stored somewhat like this:
IX_COLA A->1, A->4, A->6, B->3, B->4, B->7, C->5, C->8
IX_COLB A->4, B->2, C->6, D->5, E->7, F->3, G->1, H->8
So you can see the database can easily walk through IX_COLA in order, or
it can walk through IX_COLB in order, but it can't walk through both in
order.
If you have "SELECT * FROM TBL WHERE COLB > B" it can walk the index on
COLB to select out C, D, E, F, G and H values quickly.
However, if you had "SELECT * FROM TBL WHERE COLA=A AND COLB > B", it
can use the index on COLA to find the three "A"s and then it's got to
look at the row data directly to see whether COLB > b.
Hope that helps!
Dean.
> Why FB does not uses both indices?Because it can't. Indexes are not magic, and for me, the easiest way to
think of an index is as a pre-sorted list of pointers to the actual rows
(sorted by the columns of the index).
Let's do a quick example. Say you've got two columns like this:
Row# COLA COLB
1 A G
2 A B
3 B F
4 B A
5 C D
6 A C
7 B E
8 C H
And a separate index on each, the indexes will be stored somewhat like this:
IX_COLA A->1, A->4, A->6, B->3, B->4, B->7, C->5, C->8
IX_COLB A->4, B->2, C->6, D->5, E->7, F->3, G->1, H->8
So you can see the database can easily walk through IX_COLA in order, or
it can walk through IX_COLB in order, but it can't walk through both in
order.
If you have "SELECT * FROM TBL WHERE COLB > B" it can walk the index on
COLB to select out C, D, E, F, G and H values quickly.
However, if you had "SELECT * FROM TBL WHERE COLA=A AND COLB > B", it
can use the index on COLA to find the three "A"s and then it's got to
look at the row data directly to see whether COLB > b.
Hope that helps!
Dean.