Sub topic 9
Clustered and Secondary Indexes
Assume you know SELECT, WHERE, ORDER BY, JOIN, tables, rows, columns, primary keys, and the difference between linear and logarithmic work.
An index can organize the table's data or point to data stored elsewhere. This distinction explains why two indexes can support the same lookup but have different storage and write behavior.
Clustered indexes
A clustered index determines the physical order of table rows. Its leaf pages contain the row data itself. Because a table can have only one physical order, a table can have at most one clustered index.
A clustered index helps queries that read rows near one another in index order. For example, a lookup for a customer and a range of dates may read nearby pages when the table is clustered by those columns. It can also reduce extra table lookups because the index already contains the row.
A clustered index does not guarantee that rows remain perfectly sorted forever. Database systems handle this differently:
- InnoDB, the MySQL storage engine, stores the table in primary-key order. The primary key is the clustered index.
- SQL Server stores table data in the leaf level of its clustered index.
- PostgreSQL normally stores rows in a heap and keeps indexes separate.
CLUSTERphysically rewrites a table using an index, but PostgreSQL does not maintain that order after later writes.
Secondary indexes
A secondary index, also called a nonclustered index, is separate from the table's main data organization. Its entries contain indexed values and a locator for the matching row.
A point lookup usually follows two steps:
- Search the secondary index.
- Use the row locator to fetch the complete row from the table.
That second step is often called a key lookup or bookmark lookup. It can be cheap for a few rows but expensive when a query returns a large part of the table.
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
created_at timestamptz NOT NULL,
total_cents integer NOT NULL
);
CREATE INDEX orders_customer_idx
ON orders (customer_id);
-- PostgreSQL physically rewrites the table in this index's order.
-- Later inserts and updates can make the order less useful.
CLUSTER orders USING orders_customer_idx;
ANALYZE orders;
EXPLAIN (COSTS OFF)
SELECT *
FROM orders
WHERE customer_id = 42;In PostgreSQL, orders_customer_idx remains a secondary index even after CLUSTER. The table's heap pages were arranged according to the index at that moment, but the index still stores row references separately.
Choosing between them
| Property | Clustered index | Secondary index |
|---|---|---|
| Number per table | At most one | Usually many |
| Leaf pages contain | Table rows | Keys and row locators |
| Best benefit | Ordered and range access | Flexible searches on other columns |
| Main cost | Page movement and organization | Extra lookup and maintenance |
Clustered order is most valuable when common queries read ranges, such as recent events or all orders for one customer. A secondary index is useful when queries need several different search paths.
A clustered index can also make inserts expensive when new rows belong in the middle of existing pages. A secondary index adds work to every insert, update of an indexed column, and delete, but it avoids changing the table's primary physical organization.
Exercise
Why can a table have only one clustered index but several secondary indexes?
Show answer
A clustered index defines one physical row order, while each secondary index provides a separate lookup path to that same table data.