liveBook · Manning

Single-column index

As explained in Just Use Postgres!

A single-column index is a database index whose key is derived from one table column.

A single-column index derives its key from one table column and lets PostgreSQL search a subset of indexed data instead of scanning every row. PostgreSQL uses a B-tree by default, while the USING clause selects another access method. section 4.10 section 4.2

Speeding up a leaderboard query

A game can index scores for range filters and descending leaderboards: CREATE INDEX idx_score ON game.player_stats(score DESC); SELECT username, score, level FROM game.player_stats ORDER BY score DESC LIMIT 5; Before indexing, filtering scores between 5,000 and 6,000 performed a sequential scan, examined 10,000 rows, and took 1.753 ms. Afterward, PostgreSQL used an Index Only Scan and took 0.350 ms; the leaderboard used an Index Scan. section 4.5.1

Choose a single-column index according to the operations the column must support:

  • Use a B-tree for equality searches, ordering, and range predicates such as >, <, and BETWEEN. section 4.5.1
  • Use a hash index when lookups use equality or IN and range queries are unnecessary. section 4.5.2
  • Expect PostgreSQL to prefer a sequential scan when a predicate matches many rows; an index is not automatically faster. section 4.6.1
  • Add an index only when workload and table size justify its maintenance cost; replace several insufficient single-column indexes with a composite index when queries combine columns. section 4.6.1 section 11.5.2

Equality lookup with a hash index

A game with only five transferable champion titles can optimize an equality lookup: CREATE INDEX idx_champion_title ON game.player_stats USING hash(champion_title); SELECT username, score, level FROM game.player_stats WHERE champion_title = 'Naval Warlord'; The query changed from a sequential scan that removed 9,999 rows to an Index Scan using idx_champion_title, reducing execution time from 1.463 ms to 0.073 ms. section 4.5.2

CREATE INDEX new_index_name ON tableA(columnA);

Use the compact CREATE INDEX pattern to adapt a single-column index to another table and column. section 4.2

How Just Use Postgres! covers it

How deep it goes
Explains creation, B-tree and hash behavior, execution plans, and planner trade-offs, while deferring broader composite-index design.
Examples
Uses game scores and champion titles, time-series timestamps, and message-queue creation times as concrete indexing cases.
What it assumes
Assumes familiarity with SQL queries and PostgreSQL tables while teaching index selection and plan inspection through runnable examples.
What you can do after
Apply PostgreSQL CREATE INDEX and EXPLAIN to create indexes and verify whether queries use them effectively.
How it connects
Connects single-column indexes to B-trees, hash indexes, composite indexes, partial indexes, EXPLAIN, sequential scans, and covering indexes.

Also explained in

Compare how 2 books explain single-column index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.