views:

425

answers:

5

I've created composite indexes (indices for you mathematical folk) on tables before with an assumption of how they worked. I was just curious if my assumption is correct or not.

I assume that when you list the order of columns for the index, you are also specifying how the indexes will be grouped. For instance, if you have columns a, b, and c, and you specify the index in that same order a ASC, b ASC, and c ASC then the resultant index will essentially be many indexes for each "group" in a.

Is this correct? If not, what will the resultant index actually look like?

+1  A: 

No. Resultant index will be single index but with compound key.

KeyX = A,B,C,D; KeyY = 1,2,3,4;

Index KeyX, KeyY will be actually: A1,A2,A3,B1,B3,C3,C4,D2

So that in case you need to find something by KeyX and KeyY - that will be fast and will use single index. Something like SELECT ... WHERE KeyX = "B" AND KeyY = 3.

But it's important to understand: WHERE KeyX = ? requests will use that index, while WHERE KeyY = ? will NOT use such index at all.

Mash
I sort of understand your explanation but the re-use of the letters can get confusing easily.
Joe Philllips
@d03boy: better now?
Mash
The last assertion isn't true on Oracle. See http://stackoverflow.com/questions/57878/sql-oracle-when-indexes-on-multiple-columns-can-be-used (ignore the - incorrect - accepted answer).
Hobo
@Hobo: 1. In most RDBMS skip scan not available. 2. In most situations that would be VERY slow, just a bit faster (and sometimes even slower) than simple table scan (in very rare situations it will really help). No magic in Oracle. Just a good rule to remember - DON'T rely on compound index if your criteria not using only top level columns from index (that's very common mistake to create large compound indexes).
Mash
@Mash Points taken. Definitely not saying skip scans are a silver bullet, just that there are circumstances where KeyY = ? _will_ use an index. Figured it's best to give as full a picture as possible. As for the speed, would hope the optimizer would pick the appropriate approach (though, as always, would measure rather than assume if in doubt)
Hobo
@Hobo. I think since it's a novice question - it's better not to give FULL picture, but smaller one at first. As of optimizer, you know - lots of studies shown that AI of Oracle optimizer smarter than it should and Oracle 10 slower most of the time than Oracle 9 in practice just because it's too smart in theory.
Mash
+2  A: 

See here: SQL Server covering indexes for a good explanation

SQLMenace
Nothing about composites? hm...
Mash
This seems like a composite index to me CREATE NONCLUSTERED INDEX idx_PeopleTest_Name_Id_FavoriteColor ON PeopleTest(Name, Id, FavoriteColor)
SQLMenace
my fault, sorry
Mash
Very clear explanation.
Joe Philllips
+4  A: 

Composite indexes work just like regular indexes, except they have multi-values keys.

If you define an index on the fields (a,b,c) , the records are sorted first on a, then b, then c.

Example:

| A | B | C |
-------------
| 1 | 2 | 3 |
| 1 | 4 | 2 |
| 1 | 4 | 4 |
| 2 | 3 | 5 |
| 2 | 4 | 4 |
| 2 | 4 | 5 |
Rik
I like the simplicity of this answer but it may benefit from a little more information.
Joe Philllips
Nice update, this makes it fairly clear.
Joe Philllips
+2  A: 

The most common implementation of indices uses B-trees to allow somewhat rapid lookups, and also reasonably rapid range scans. It's too much to explain here, but here's the Wikipedia article on B-trees. And you are right, the first column you declare in the create index will be the high order column in the resulting B-tree.

A search on the high order column amounts to a range scan, and a B-tree index can be very useful for such a search. The easiest way to see this is by analogy with the old card catalogs you have in libraries that have not yet converted to on line catalogs.

If you are looking for all the cards for Authors whose last name is "Clemens", you just go to the author catalog, and very quickly find a drawer that says "CLE- CLI" on the front. That's the right drawer. Now you do a kind of informal binary search in that drawer to quickly find all the cards that say "Clemens, Roger", or "Clemens, Samuel" on them.

But suppose you want to find all the cards for the authors whose first name is "Samuel". Now you're up the creek, because those cards are not gathered together in one place in the Author catalog. A similar phenomenon happens with composite indices in a database.

Different DBMSes differ in how clever their optimizer is at detecting index range scans, and accurately estimating their cost. And not all indices are B-trees. You'll have to read the docs for your specific DBMS to get the real info.

Walter Mitty
+3  A: 

Composite index is like a plain alphabet index in a dictionary, but covering two or more letters, like this:

AA - page 1
AB - page 12

etc.

Table rows are ordered first by the first column in the index, then by the second one etc.

It's usable when you search by both columns OR by first column. If your index is like this:

AA - page 1
AB - page 12
…
AZ - page 245
BA - page 246
…

you can use it for searching on 2 letters ( = 2 columns in a table), or like a plain index on one letter:

A - page 1
B - page 246
…

Note that in case of a dictionary, the pages themself are alphabetically ordered. That's an example of a CLUSTERED index.

In a plain, non-CLUSTERED index, the references to pages are ordered, like in a history book:

Gaul, Alesia: pages 12, 56, 78
Gaul, Augustodonum Aeduorum: page 145
…
Gaul, Vellaunodunum: page 24
Egypt, Alexandria: pages 56, 194, 213, 234, 267

Composite indexes may also be used when you ORDER BY two or more columns. In this case a DESC clause may come handy.

See this article in my blog about using DESC clause in a composite index:

Quassnoi