Difficulty: Advanced
What is the difference between a clustered and a non-clustered index? How does each affect query performance?
A clustered index defines the order in which the table's rows are physically stored: the leaf level of the index is the table data, so a table can have only one. A non-clustered (secondary) index is a separate structure holding the indexed columns plus a pointer back to the row, and a table can have many. Lookups through a clustered index land directly on the data, while a non-clustered index usually needs an extra hop to fetch the remaining columns.
Both are typically B+trees. The difference is what sits in the leaves:
UNIQUE NOT NULL index, or a hidden 6-byte row ID). In SQL Server you choose the clustered index; by default it is the primary key.| Aspect | Clustered | Non-clustered |
|---|---|---|
| Count per table | One | Many |
| Leaf contents | Entire row | Key columns + row locator |
| Range scans on the key | Very fast, rows are contiguous | Can mean many random lookups |
| Extra storage | None beyond the table | Separate structure per index |
| Insert cost | Page splits if keys are not monotonic | One extra tree to maintain per index |
Range queries like WHERE created_at BETWEEN ... on a clustered key read neighbouring pages sequentially. The same query through a non-clustered index may do one key lookup per matching row, which is why optimizers often switch to a full scan when a filter matches more than a small fraction of the table.
If the non-clustered index contains every column a query needs, the engine answers from the index alone. SQL Server and Postgres (11+) support INCLUDE columns for this; Postgres reports it as an Index Only Scan, MySQL as "Using index" in EXPLAIN.
Postgres has no true clustered index. Tables are heaps and every index, including the primary key, is secondary and points to a tuple ID (ctid). The CLUSTER command reorders the heap once, but the order is not maintained for new writes. Mentioning this in an interview signals real hands-on experience.
Pick something narrow, unique, stable and ever-increasing (a BIGINT identity or a time-ordered ID like UUIDv7). Random keys like UUIDv4 cause page splits and fragmentation, and because InnoDB secondary indexes store the primary key, a wide key bloats every index on the table.
| Clustered (InnoDB PK) | Secondary index | |
|---|---|---|
| Tree walk 1 | Primary key B+tree | Secondary B+tree on email |
| Leaf holds | The full row | email + primary key value |
| Tree walk 2 | Not needed | Primary key B+tree to fetch row |
| Skip walk 2 when | Always skipped | Index covers all selected columns |
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY, -- clustered index
email VARCHAR(255) NOT NULL,
name VARCHAR(100),
city VARCHAR(50),
INDEX idx_email (email), -- secondary, stores (email, id)
INDEX idx_city_name (city, name) -- secondary, stores (city, name, id)
);
-- One B+tree walk: clustered index leaf is the row
SELECT * FROM users WHERE id = 42;
-- Two walks: idx_email -> id -> clustered index
SELECT * FROM users WHERE email = 'asha@example.com';
-- Covering: answered from idx_city_name alone ("Using index")
EXPLAIN SELECT name, id FROM users WHERE city = 'Pune';
Because InnoDB secondary indexes store the primary key, selecting only indexed columns plus id avoids the second lookup entirely.
CREATE INDEX idx_orders_user ON orders (user_id) INCLUDE (status, total_paise);
EXPLAIN SELECT status, total_paise FROM orders WHERE user_id = 7;
-- Index Only Scan using idx_orders_user on orders
INCLUDE columns are stored in the leaf but are not part of the search key, so the query never touches the heap if the visibility map is up to date.
Clustered Index, Non-clustered Index, B+Tree, Key Lookup, Covering Index