An index on two columns can be created with either of the statements
create index foo_ix on foo(a,b); create index foo_ix on foo(b,a);
How does this affect the operational (runtime) characteristics of using the index?
How does this affect the layout (physical) characteristics of the index?
Are either (1) or (2) affected by the types/sizes of the columns?
What are the best practices for creating multi-column indexes?
In short, does it matter which column I put first?
Selectivity of the individual columns in a composite index does not matter when picking the order. Here is the simple thought process: Effectively, an index is the concatenation of the columns involved.
Using ORDER BY on indexed column is not a good idea. Actually the purpose of using index is to making searching faster so the index column helps to maintain the data in sorted order.
Yes, index will help you, when using ORDER BY. Because INDEX is a sorted data structure, so the request will be executed faster. Look at this example: table test2 with 3 rows.
Execution is most efficient when you create a composite index with the columns in order from most to least distinct. In other words, the column that returns the highest count of distinct rows when queried with the DISTINCT keyword in the SELECT statement should come first in the composite index.
a
and b
both have 1000 distinct values and they are always queried together then the order of columns in the index doesn't really matter. But if a
has only 10 distinct values or you have queries which use just one of the columns then it does matter; in these scenarios the index may not be used if the column ordering does not suit the query.The one potential exception to 2. and 3. is with DATE columns. Because Oracle DATE columns include a time element they might have 86400 distinct values per day. However most queries on a data column are usually only interested in the day element, so you might want to consider only the number of distinct days in your calculations. Although I suspect it won't affect the relative selectivity in but a handful of cases.
edit (in response to Nick Pierpoint's comment)
The two main reasons for leading with the least selective column are
Both these work their magic from knowing that the value in the current slot is the same as the value in the previous slot. Consequently we can maximize the return from these techniques by minimsing the number of times the value changes. In the following example, A
has four distinct values and B
has six. The dittos represent a compressible value or a skippable index block.
Least selective column leads ... A B --------- - AARDVARK 1 " 2 " 3 " 4 " 5 " 6 DIFFVAL 1 " 2 " 3 " 4 " 5 " 6 OTHERVAL 1 " 2 " 3 " 4 " 5 " 6 WHATEVER 1 " 2 " 3 " 4 " 5 " 6
Most selective column leads ...
B A - -------- 1 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER 2 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER 3 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER 4 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER 5 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER 6 AARDVARK " DIFFVAL " OTHERVAL " WHATEVER
Even in this trival example, (A, B)
has 20 skippable slots compared to the 18 of (B, A)
. A wider disparity would generate greater ROI on index compression or better utility from Index Skip reads.
As is the case with most tuning heuristics we need to benchmark using actual values and realistic volumes. This is definitely a scenario where data skew could have a dramatic impact of the effectiveness of different approaches.
"I think if you have a highly selective first index then - from a performance perspective - you'll do well to put it first."
If we have a highly selective column then we should build it an index of its own. The additional benefits of avoiding a FILTER operation on a handful of rows is unlikely to be outweighed by the overhead of maintaining a composite index.
Multi-column indexes are most useful when we have:
But according to Oracle itself, it's better to put the column with the highest cardinality first:
http://docs.oracle.com/cd/B10500_01/server.920/a96533/data_acc.htm#2174
Ordering Keys for Composite Indexes
If all keys are used in WHERE clauses equally often, then ordering these keys from most selective to least selective in the CREATE INDEX statement best improves query performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With