A shortcut so the database does not read every row to find the one you want.
An index is a separate, sorted structure that lets the database find rows without reading the whole table. It works like the index at the back of a book.
A table is stored in the order rows were added. To find "orders for customer 42", the database would have to look at every row. An index keeps a small, sorted copy of one or more columns, each entry pointing to the row. Because it is sorted, the database can jump to the right place, like a binary search, instead of reading everything.
Most relational databases use a B-tree (usually the B+ tree variant) as the default index. It is a shallow, wide tree: each page holds hundreds of keys, so even a table with a billion rows needs only about 4 or 5 page reads to reach one row.
| Kind | What it is | Good for |
|---|---|---|
| B-tree (B+ tree) | A sorted, balanced tree. The default. | Equals, ranges, sorting, prefix match |
| Hash index | A hash table from key to row. | Equals only. Not ranges or sorting. |
| Composite (multi-column) | One index over several columns in a fixed order. | Queries that filter on those columns together |
| Unique index | An index that also rejects duplicates. | Email, username, natural keys |
| Covering index | Holds every column the query needs, so the table is never read. | Hot queries that read few columns |
| Partial (filtered) index | Indexes only rows that match a condition. | WHERE status = 'ACTIVE' when most rows are old |
| Clustered index | The table itself is stored in index order (one per table). | Range scans on the primary key |
| Inverted, GIN, geo, vector | Specialised structures for words, arrays, JSON, maps, embeddings. | Search, tags, location, similarity |
A composite index on (country, status, day) is sorted by country first, then status inside a country, then day. It helps queries that start from the left: country; country + status; country + status + day. It does not help a query on status alone, because statuses are scattered across all countries. A range on one column (day between...) ends how far into the index the search can narrow, so put equality columns first and the range column last.
CREATE INDEX idx_orders_country_status_day ON orders (country, status, day); -- uses the index (left-most prefix): SELECT * FROM orders WHERE country = 'IN' AND status = 'PAID'; -- cannot use it well (skips the first column): SELECT * FROM orders WHERE status = 'PAID'; -- see what the database chose: EXPLAIN SELECT * FROM orders WHERE country = 'IN';
1. Table scan vs index lookup
2. Composite index: column order matters (10M row orders table)
WHERE country = 'IN' AND status = 'PAID'
3. Every extra index slows writes
Simplified models with fixed maths (no random numbers). The tree uses 200 keys per page. In section 2 we assume equal spread of values: 50 countries, 5 statuses, 365 days. Real planners use statistics and may choose differently. Part 3 uses made-up relative costs to show the shape of the trade-off, not real numbers.
| Term | Meaning in easy words |
|---|---|
| Index | A sorted structure that helps find rows fast |
| Full table scan | Reading every row of a table |
| B-tree / B+ tree | A balanced, wide tree used by most indexes |
| Selectivity | How much a filter narrows the result. Higher selectivity means fewer rows match |
| Composite index | An index on several columns, in order |
| Left-most prefix | A composite index helps only queries that use its first columns |
| Covering index | An index that holds all columns a query needs |
| Clustered index | The index that decides how the table rows are stored |
| Cardinality | The number of distinct values in a column |
| EXPLAIN | A command that shows how the database will run a query |
| Choice | We gain | We lose |
|---|---|---|
| More indexes | More query shapes are fast | Slower writes, more space |
| Fewer indexes | Fast writes, less space | Slow reads for the missing paths |
| Wide composite index | Covers many queries, may avoid table reads | Large and costly on every write |
| Covering index | No table lookup at all | Bigger index to store and update |
| Partial index | Small and cheap | Only helps queries with the same condition |