Clustered vs Non-Clustered Index: Difference Explained
A clustered index stores the rows in key order. A secondary index stores the key and a pointer back. How a lookup uses both, in InnoDB and PostgreSQL.
In this chapter
Clustered and secondary indexes
Clustered vs non-clustered indexes: common questions
What is the difference between a clustered and a non-clustered index?
A clustered index is the table itself, stored in key order, so there can be only one. A non-clustered (secondary) index is a separate structure holding the key plus a pointer to the row.
How many clustered indexes can a table have?
One, because rows can be physically ordered in only one way. In InnoDB it is the primary key.
Does PostgreSQL have clustered indexes?
No. PostgreSQL stores rows in a heap. CLUSTER reorders the table once, but new rows do not keep that order.
More on Indexes: how they are built
Keep learning
Overview
Raj Patel Ana Ruiz Zoe Li Bob Lee Mia Chen Cy Park Grace Kim Ted Ng < mia@x | ≥ mia@x leaf: email → id leaf: full rows By primary key One tree walk. The clustered leaf is the row, so the search ends there. By secondary index Two tree walks. The email leaf only knows id 7, so InnoDB walks the clustered index again to fetch name. The email leaf already holds email and id, every column the query asks for. No second walk. EXPLAIN shows "Using index". No usable index No index starts with name. MySQL reads every clustered leaf and checks each row. Fine for 8 rows, slow for 8 million. A secondary index on email and the clustered primary key index, with the pages this query reads highlighted. ui-monospace, monospace INDEX (email) · secondary PRIMARY KEY (id) · clustered id 7 → look up again Numbered pages are the reads, in order. Real trees have 3–4 levels and millions of rows, but the shape is the same. Pick at least one column for the WHERE clause. multi cannot be used for this search The index is sorted by first_name first. Without it, matching rows are spread across the whole tree, like finding every "Sharma" in a phone book sorted by first name. Searching by email alone needs its own index, for example INDEX (email). Every condition is part of the leftmost prefix, so the tree jumps straight to the matching entries. INDEX multi (first_name, last_name, email). Tick the columns in your WHERE clause: Index columns of multi (as SHOW INDEX lists them) in WHERE narrows the search not in WHERE index unusable checked afterwards chapters · indexes Clustered and secondary indexes: why some lookups take twice the work
1. Two kinds of tree: the dictionary and the book index
Every InnoDB table has exactly one clustered index, the table itself, and any number of secondary indexes beside it.
The index at the back
A separate sorted list of topic → page number. Finding the topic is fast, then you turn to the page. A book can have many such lists. In MySQL InnoDB which one PRIMARY KEY every other INDEX / UNIQUE leaf holds the whole row the indexed columns + the primary key how many exactly one as many as you create inserts in the middle can split pages extra storage, and every write updates it too
2. Follow a lookup through both trees
The people table has a clustered index on id and a secondary index on email. Pick a query and count the pages. By id, the walk ends on the row. By email, the walk ends on a primary key value, and a second walk down the clustered tree fetches the row.
3. An index on several columns is read from the left
The index multi is on (first_name, last_name, email). Like a phone book sorted by surname then first name, it is sorted by first_name, then by last_name inside each first_name, then by email. So it helps only a search that starts from the left: first_name, or first_name + last_name, or all three. A search on last_name alone cannot use it. Try a few combinations. ⇥ Order the columns by how you search
4. Check what a table has: SHOW INDEXES
MySQL lists one line per indexed column. Here the 486,971-row people table has three indexes: the primary key, the three-column multi from section 3, and a full-text index on bio. Selected columns of the output dupes ok Non_unique 0 means duplicates are rejected. PRIMARY is always 0. Seq_in_index Three rows named multi are one composite index. 1, 2, 3 is its column order. Cardinality An estimate of distinct values. For a composite index it counts distinct prefixes. first_name alone has 2,945 distinct values, so one name still matches about 165 rows on average. Adding last_name makes almost every pair unique. Higher cardinality means the index narrows the search more.
5. Not every database clusters: heap tables
InnoDB stores the table inside the primary-key tree. Not every database does. A heap table keeps rows in the order they were inserted, and every index, the primary key included, stores the physical location of the row. A lookup finds the entry and then jumps to that location. Where the row lives Clustered (InnoDB) Heap (PostgreSQL) The table is The primary-key B+ tree An unsorted pile of pages, rows in insertion order An index entry holds The key and the primary key value The key and the row's location (page and slot) Lookup by primary key One walk. The leaf is the row. One walk, then a jump to the heap page Lookup by another index Walk it, then walk the primary-key tree Walk it, then a jump to the heap page An insert goes To its place in key order To the end of the table, whatever the key Which databases MySQL always. SQL Server by default for a primary key. Oracle if you ask (an index-organized table). PostgreSQL always. Oracle by default. SQL Server when a table has no clustered index. Clustering mostly helps the primary key lookup, where the row is already in the leaf. For other indexes both designs need one more step to load the row, so there is little difference. The heap pays elsewhere: a row that moves leaves every index with a stale location, so an update that cannot stay on its page needs a new entry in each index. The key you choose matters most for clustered tables, as primary-key order shows. PostgreSQL's side is in PostgreSQL index types.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.