3.6 Multi-Column Indexes
When you build an index over several columns, column order matters. An index built in (A, B) order can fix the search range knowing only the value of A, and when both A and B are known it narrows to a smaller range. Knowing only the value of B, however, does not give a starting point for the search, because the leading column is the first sort key. This is why the choice of leading column matters when designing a composite index.
An index built over two or more columns is called a multi-column index (composite index). For example, if the orders table is often queried for "a given customer's most recent orders," you create an index like the following.
CREATE INDEX idx_orders_cust_date ON orders (customer_id, order_date);
The leaves of this index are sorted first by customer_id, and only entries with the same customer_id are sorted by order_date. The key point is that the trailing column is sorted only within the range where the leading column's value is the same.
Figure 3-7. Sort order of a multi-column index and search range by condition
block-beta
columns 9
t["leaf entries of (customer_id, order_date), in index order"]:9
hn["#"] hc["cid"] hd["date"] q1["(1) cid=1002"]:2 q2["(2) + date>='10-01'"]:2 q3["(3) date='10-05'"]:2
n1["1"] c1["1001"] d1["09-12"] space:2 space:2 space:2
n2["2"] c2["1001"] d2["10-05"] space:2 space:2 m3_2["match"]:2
n3["3"] c3["1001"] d3["10-20"] space:2 space:2 space:2
n4["4"] c4["1002"] d4["09-03"] m1_4["start"]:2 space:2 space:2
n5["5"] c5["1002"] d5["09-28"] m1_5["|"]:2 space:2 space:2
n6["6"] c6["1002"] d6["10-01"] m1_6["|"]:2 m2_6["start"]:2 space:2
n7["7"] c7["1002"] d7["10-05"] m1_7["|"]:2 m2_7["|"]:2 m3_7["match"]:2
n8["8"] c8["1002"] d8["10-17"] m1_8["stop"]:2 m2_8["stop"]:2 space:2
n9["9"] c9["1003"] d9["09-15"] space:2 space:2 space:2
n10["10"] c10["1003"] d10["10-05"] space:2 space:2 m3_10["match"]:2
n11["11"] c11["1003"] d11["10-09"] space:2 space:2 space:2
class t plain
class hn,hc,hd,q1,q2,q3 meta
class n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11 muted
class c1,d1,c2,d2,c3,d3,c4,d4,c5,d5,c6,d6,c7,d7,c8,d8,c9,d9,c10,d10,c11,d11 data
class m1_5,m1_6,m1_7,m2_7 slot
class m3_2,m1_4,m2_6,m3_7,m1_8,m2_8,m3_10 hot
The three columns on the left list the entries stored in the index leaves in index order (the values are examples for illustration). The cid (customer_id) column is sorted 1001, 1002, 1003 from top to bottom, and the date (order_date) column is in date order only within a group of the same customer_id. You can see that after the 1001 group ends at 10-20, the 1002 group starts again from 09-03. The three columns on the right show, for each condition, where in the index reading starts (start) and where it stops (stop), or where the matching entries are (match).
Let us look at the three conditions in the figure in turn.
- Searching by the leading column (
customer_id = 1002): The entries with customer_id 1002 sit together in one place in the index (entries 4 to 8). The database descends the B+Tree to find the first entry (4), scans the leaf pages sequentially, and stops where 1002 ends (8). - Using both the leading and trailing columns (
customer_id = 1002 AND order_date >= '10-01'): Within the 1002 section the dates are sorted, so the starting point moves up to entry 6 and the range narrows further. This is the most efficient case, using both columns. - Searching by the trailing column only (
order_date = '10-05'): 10-05 is scattered at a different position for each customer (entries 2, 7, and 10). Since no starting point can be determined, the database either scans the index from beginning to end or skips the index and does a full scan of the table.
In this way, a multi-column index does its job only when conditions are given starting from the leading column in order. This is commonly called the leftmost prefix rule. An index on (customer_id, order_date) can stand in for an index built on customer_id alone, but cannot stand in for an index built on order_date alone.
Criteria for choosing column order
- Put columns searched with equality (=) first, and columns searched with ranges (>, <, BETWEEN) after. A column that follows a range-conditioned column can hardly make use of the sort order.
- Put the column most often used alone first. This is because the index is also used for lookups that have only the leading column.
- Building it in the same order as ORDER BY lets you skip the sort. For
WHERE customer_id = 1002 ORDER BY order_date, simply reading the index in order already gives date order.
Note that some databases provide a feature, such as Oracle's index skip scan, that partially uses the index with only a trailing-column condition when the leading column has very few distinct values. This is an exceptional case, however, so the leftmost prefix rule should be the baseline when designing.