Even though the database creates the index for the primary key automatically, there is still room for manual refinements if the key consists of multiple columns. In that case the database creates an index on all primary key columns—a so-called concatenated index (also known as multi-column, composite or combined index). Note that the column order of a concatenated index has great impact on its usability so it must be chosen carefully.
For the sake of demonstration, let’s assume there is a company merger. The employees of the other company are added to our EMPLOYEES
table so it becomes ten times as large. There is only one problem: the EMPLOYEE_ID
is not unique across both companies. We need to extend the primary key by an extra identifier—e.g., a subsidiary ID. Thus the new primary key has two columns: the EMPLOYEE_ID
as before and the SUBSIDIARY_ID
to reestablish uniqueness.
The index for the new primary key is therefore defined in the following way:
CREATE UNIQUE INDEX employees_pk
ON employees (employee_id, subsidiary_id)
A query for a particular employee has to take the full primary key into account—that is, the SUBSIDIARY_ID
column also has to be used:
SELECT first_name, last_name
FROM employees
WHERE employee_id = 123
AND subsidiary_id = 30
Whenever a query uses the complete primary key, the database can use an INDEX UNIQUE SCAN
—no matter how many columns the index has. But what happens when using only one of the key columns, for example, when searching all employees of a subsidiary?
SELECT first_name, last_name
FROM employees
WHERE subsidiary_id = 20
The execution plan reveals that the database does not use the index. Instead it performs a TABLE ACCESS FULL
. As a result the database reads the entire table and evaluates every row against the where
clause. The execution time grows with the table size: if the table grows tenfold, the TABLE ACCESS FULL
takes ten times as long. The danger of this operation is that it is often fast enough in a small development environment, but it causes serious performance problems in production.
The database does not use the index because it cannot use single columns from a concatenated index arbitrarily. A closer look at the index structure makes this clear.
On my Own Behalf
I offer training, tuning and consulting. Buying my book “SQL Performance Explained” (from €9.95) also supports my work on this website.
A concatenated index is just a B-tree index like any other that keeps the indexed data in a sorted list. The database considers each column according to its position in the index definition to sort the index entries. The first column is the primary sort criterion and the second column determines the order only if two entries have the same value in the first column and so on.
Important
A concatenated index is one index across multiple columns.
The ordering of a two-column index is therefore like the ordering of a telephone directory: it is first sorted by surname, then by first name. That means that a two-column index does not support searching on the second column alone; that would be like searching a telephone directory by first name.
Figure 2.1 Concatenated Index

The index excerpt in Figure 2.1 shows that the entries for subsidiary 20 are not stored next to each other. It is also apparent that there are no entries with SUBSIDIARY_ID = 20
in the tree, although they exist in the leaf nodes. The tree is therefore useless for this query.
Tip
Visualizing an index helps in understanding what queries the index supports. You can query the database to retrieve the entries in index order (SQL:2008 syntax, see syntax of top-n queries for proprietary solutions using LIMIT
, TOP
or ROWNUM
):
SELECT <INDEX COLUMN LIST>
FROM <TABLE>
ORDER BY <INDEX COLUMN LIST>
FETCH FIRST 100 ROWS ONLY
If you put the index definition and table name into the query, you will get a sample from the index. Ask yourself if the requested rows are clustered in a central place. If not, the index tree cannot help find that place.
We could, of course, add another index on SUBSIDIARY_ID
to improve query speed. There is however a better solution—at least if we assume that searching on EMPLOYEE_ID
alone does not make sense.
We can take advantage of the fact that the first index column is always usable for searching. Again, it is like a telephone directory: you don’t need to know the first name to search by last name. The trick is to reverse the index column order so that the SUBSIDIARY_ID
is in the first position:
CREATE UNIQUE INDEX EMPLOYEES_PK
ON EMPLOYEES (SUBSIDIARY_ID, EMPLOYEE_ID)
Both columns together are still unique so queries with the full primary key can still use an INDEX UNIQUE SCAN
but the sequence of index entries is entirely different. The SUBSIDIARY_ID
has become the primary sort criterion. That means that all entries for a subsidiary are in the index consecutively so the database can use the B-tree to find their location.
Important
The most important consideration when defining a concatenated index is how to choose the column order so it can be used as often as possible.
The execution plan confirms that the database uses the “reversed” index. The SUBSIDIARY_ID
alone is not unique anymore so the database must follow the leaf nodes in order to find all matching entries: it is therefore using the INDEX RANGE SCAN
operation.
- DB2
Explain Plan ------------------------------------------------------------- ID | Operation | Rows | Cost 1 | RETURN | | 128 2 | FETCH EMPLOYEES | 1195 of 1195 (100.00%) | 128 3 | RIDSCN | 1195 of 1195 (100.00%) | 43 4 | SORT (UNIQUE) | 1195 of 1195 (100.00%) | 43 5 | IXSCAN EMPLOYEES_PK | 1195 of 10000 ( 11.95%) | 43 Predicate Information 2 - SARG (Q1.SUBSIDIARY_ID = +00002.) 5 - START (Q1.SUBSIDIARY_ID = +00002.) STOP (Q1.SUBSIDIARY_ID = +00002.)
This execution plan looks more complex than the execution plan that was using the index before. They key operations are still there, however: the
IXSCAN
representing the index range scan and theFETCH
for the table access. In between these operations there is an unexpectedSORT
andRIDSCN
operation: theSORT
operation sorts the entries fetched from the index according to the rows physical storage location in the heap table. TheRIDSCAN
then prefetches all the affected database pages (collapsing multiple adjacent blocks into a single IO operation).- MySQL
+----+-----------+------+---------+---------+------+-------+ | id | table | type | key | key_len | rows | Extra | +----+-----------+------+---------+---------+------+-------+ | 1 | employees | ref | PRIMARY | 5 | 123 | | +----+-----------+------+---------+---------+------+-------+
The MySQL access type
ref
is the equivalent ofINDEX RANGE SCAN
in the Oracle database.- Oracle
--------------------------------------------------------------- |Id |Operation | Name | Rows | Cost | --------------------------------------------------------------- | 0 |SELECT STATEMENT | | 106 | 75 | | 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 106 | 75 | |*2 | INDEX RANGE SCAN | EMPLOYEES_PK | 106 | 2 | --------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("SUBSIDIARY_ID"=20)
- PostgreSQL
QUERY PLAN ---------------------------------------------- Bitmap Heap Scan on employees (cost=24.63..1529.17 rows=1080 width=13) Recheck Cond: (subsidiary_id = 2::numeric) -> Bitmap Index Scan on employees_pk (cost=0.00..24.36 rows=1080 width=0) Index Cond: (subsidiary_id = 2::numeric)
The PostgreSQL database uses two operations in this case: a
Bitmap Index Scan
followed by aBitmap Heap Scan
. They roughly correspond to Oracle’sINDEX RANGE SCAN
andTABLE ACCESS BY INDEX ROWID
with one important difference: it first fetches all results from the index (Bitmap Index Scan
), then sorts the rows according to the physical storage location of the rows in the heap table and than fetches all rows from the table (Bitmap Heap Scan
). This method reduces the number of random access IOs on the table.- SQL Server
|--Nested Loops(Inner Join) |--Index Seek(OBJECT:employees_pk, | SEEK:subsidiary_id=20 | ORDERED FORWARD) |--RID Lookup(OBJECT:employees, SEEK:Bmk1000=Bmk1000 LOOKUP ORDERED FORWARD)
In general, a database can use a concatenated index when searching with the leading (leftmost) columns. An index with three columns can be used when searching for the first column, when searching with the first two columns together, and when searching using all columns.
Even though the two-index solution delivers very good select
performance as well, the single-index solution is preferable. It not only saves storage space, but also the maintenance overhead for the second index. The fewer indexes a table has, the better the insert
, delete
and update
performance.
On my Own Behalf
I offer training, tuning and consulting. Buying my book “SQL Performance Explained” (from €9.95) also supports my work on this website.
To define an optimal index you must understand more than just how indexes work—you must also know how the application queries the data. This means you have to know the column combinations that appear in the where
clause.
Defining an optimal index is therefore very difficult for external consultants because they don’t have an overview of the application’s access paths. Consultants can usually consider one query only. They do not exploit the extra benefit the index could bring for other queries. Database administrators are in a similar position as they might know the database schema but do not have deep insight into the access paths.
The only place where the technical database knowledge meets the functional knowledge of the business domain is the development department. Developers have a feeling for the data and know the access path. They can properly index to get the best benefit for the overall application without much effort.