Three index shapes
- Single-column —
CREATE INDEX i ON t(col). Use for any column you filter on frequently. - Composite —
CREATE INDEX i ON t(a, b). Order matters: the index helps queries filtering onaalone, ora AND b, but NOTbalone. Think of the index entries as sorted by (a,b) — you can binary-search by a, then by b within a group. - Unique —
CREATE UNIQUE INDEX i ON t(col). Combines an index with a uniqueness constraint. Used implicitly byUNIQUEcolumn declarations.
Tip: When choosing composite-index column order, put the highest-selectivity column first (the one with the most distinct values), unless your most common query filters by a less-selective leading column.