What is database indexing? A database index is an additional data structure that gives a query planner another route to matching or ordered rows. For suitable queries, it may avoid examining every table row.

Its structure, storage, and usefulness depend on the database and index type. Creating one never guarantees a faster query.

What is database indexing?

Think of an index as a lookup path beside the table. In PostgreSQL, a common B-tree index sorts keys. That order can help equality and range searches.

Other index types serve other needs. The planner compares the index path with a full table scan.

Small tables may still favor a scan. So may queries that return many rows.

PostgreSQL’s index introduction explains the benefit and the cost. The index has to stay in sync as data changes.

A small SQL example

Suppose an orders table has customer_id and created_at. First inspect the filter you actually run:

EXPLAIN SELECT id, created_at
FROM orders
WHERE customer_id = 42;

Then, in a safe test environment, create one candidate index and inspect the same query again:

CREATE INDEX orders_customer_id_idx ON orders (customer_id);
EXPLAIN SELECT id, created_at
FROM orders
WHERE customer_id = 42;

This is a possible plan change, not a speed test. If many rows share that customer ID, the new path may help little. Table size and statistics also affect the plan.

Use sample data that looks like your real workload. MySQL’s index guide also weighs index benefits against their costs.

Crunchy Data diagram comparing sequential, index, bitmap, parallel, and index-only scans in PostgreSQL

How to check the query plan

In PostgreSQL, look for Seq Scan, Index Scan, or Bitmap Index Scan. Check row estimates and the filter at each node.

EXPLAIN shows a planned path. EXPLAIN (ANALYZE, BUFFERS) runs the statement and shows work done.

That can change data for a write statement. Test with care, away from a busy production system. PostgreSQL explains the plan nodes and risks.

SQLite’s EXPLAIN QUERY PLAN uses SCAN and SEARCH. It can also name the chosen index.

These words describe SQLite’s own plan format. See the SQLite guide.

For a broad tour, watch the 2018 PyCon AU talk on PostgreSQL indexing. Use current vendor docs for version-specific choices.

When a composite index helps

Suppose a common query filters one customer and a time range. Test CREATE INDEX orders_customer_created_idx ON orders (customer_id, created_at);.

This index sorts by customer first, then time within each customer. That order matches this query well in principle.

A PostgreSQL multicolumn B-tree is often most useful when the query limits its first key. Here that key is customer_id. A time-only filter may gain less.

PostgreSQL can sometimes use skip scan, so this is no hard ban. Check the PostgreSQL 18 multicolumn guide and your plan.

Redrock diagram of B-tree entries organized first by employee ID and then by subsidiary ID

What extra indexes cost

Every extra index takes space. It also adds upkeep when indexed data changes. Some reads may improve while writes take more work.

Duplicate or rarely used indexes are hard to justify.

SQL Server’s index design guide warns about too many indexes on tables with frequent writes. Exact costs vary with the engine and workload.

A safe test checklist

  1. Choose a representative slow query and realistic data volume.
  2. Record its plan, timing, row estimates, and relevant read workload.
  3. Check predicates, joins, ordering, existing indexes, and statistics before proposing one new index.
  4. Test that candidate in a safe environment; compare plans and repeated measurements, including write and storage effects.
  5. Keep it only if the whole workload benefits. Revisit usage as queries and data change.

Five-stage illustration of inspecting a query, reading its plan, testing an index, measuring, and deciding whether to keep it

PostgreSQL’s index-usage guide says to inspect real queries. Keep table statistics current too, and separate query access paths from application-level caching and idempotent write retries. An index is a test to measure, not a blanket fix for slow SQL.