Database Indexing Basic Concepts

In a relational database an index is a dedicated data structure that maps one or more column values (the key) to the physical location of the corresponding row. Think of it as a book’s index: instead of scanning every page, you jump straight to the relevant entry. Most modern engines implement this structure as a B‑Tree, but hash tables, bitmaps, inverted lists, and spatial trees are also common.

  • Key values – the combination of columns that the index is built on.
  • Pointers – references (row IDs, block addresses, or primary‑key values) that let the engine retrieve the full row.
  • Internal organization – the algorithm that keeps the keys ordered and searchable (e.g., balanced tree nodes, hash buckets, bitmap vectors).

By keeping the keys in a sorted or otherwise searchable form, the engine can replace a full‑table scan with a logarithmic‑time lookup, dramatically cutting I/O and CPU usage.


How Indexes Are Used During Query Execution

  1. Parsing – The SQL parser turns the statement into a logical tree of operations.
  2. Costing – The optimizer consults statistics (row counts, value distribution, index depth) to estimate the cost of alternative plans.
  3. Selection – If an index can satisfy a filter (WHERE), a sort (ORDER BY), a grouping (GROUP BY), or a uniqueness check, the optimizer adds it to the candidate set.
  4. Execution – The engine walks the index structure to locate matching keys, then optionally “looks up” the full rows (the table access step) if the needed columns are not covered by the index itself.

Tip: When the predicate does not align with any index, or when the result set represents a large fraction of the table, the optimizer may still favor a full scan because the overhead of jumping between index and table outweighs the benefit.


Common Index Types – When to Use Which

Index Type Ideal Use‑Case Core Characteristics Typical Implementations
B‑Tree Range scans, ordering, equality lookups Multi‑level balanced tree, supports prefix matching, ordered traversal MySQL InnoDB, PostgreSQL default
Hash Pure equality predicates Direct bucket lookup, O(1) access, no ordering PostgreSQL hash index, Redis auxiliary structures
Bitmap Low‑cardinality columns, data‑warehouse workloads Bit‑vector compression, fast set operations (AND/OR) Oracle Bitmap, ClickHouse
Full‑Text Natural‑language search, LIKE '%word%' patterns Inverted list of terms, tokenization, relevance scoring MySQL FULLTEXT, Elasticsearch
Spatial Geometric data, GIS queries R‑Tree or QuadTree, supports bounding‑box searches MySQL Spatial, PostGIS

Each type trades off storage overhead, maintenance cost, and query capabilities. Choosing the right one hinges on the specific query patterns of your application.


Benefits and Drawbacks

Advantages

  • Reduced I/O – Especially noticeable on spinning disks; even SSDs benefit from fewer random reads.
  • Fast sorting & grouping – An ordered index can satisfy ORDER BY or GROUP BY without an extra sort step.
  • Uniqueness enforcement – Primary‑key and unique constraints are implemented as indexes.
  • Improved concurrency – Index‑level locking is finer‑grained than table‑wide locks.

Limitations

  • Write amplification – INSERT, UPDATE, and DELETE must also modify every affected index, increasing latency.
  • Space consumption – Index files often occupy 10 %–30 % of the base table size.
  • Staleness risk – Out‑of‑date statistics or skewed data distributions can cause the optimizer to pick a sub‑optimal plan.
  • Ineffectiveness on low‑selectivity predicates – If a filter matches most rows, the index provides little benefit.

Guiding Principles for Index Selection

  1. Filter first – Columns that appear most often in WHERE clauses should be indexed.
  2. Match the sort order – Align ORDER BY/GROUP BY columns with the index column sequence to avoid extra sorting.
  3. Prefer high selectivity – The higher the ratio of distinct values to total rows, the more the index can prune data.
  4. Cover the query – If all columns referenced by a query exist in the index, the engine can return results directly from the index (“covering index”), eliminating the table lookup.
  5. Observe the left‑most prefix rule – For composite indexes, the optimizer can use the index only up to the first column that is not referenced in the query.
  6. Eliminate redundancy – Compare single‑column indexes against composite ones; keep only the most cost‑effective set.

Indexes in the Bigger Picture of Query Optimization

  • Cost‑based decisions – The optimizer treats an index as a major cost factor. Accurate statistics (ANALYZE, VACUUM ANALYZE) are essential for reliable estimates.
  • Complex joins – Properly indexed join keys can turn a potentially quadratic nested‑loop join into a linear hash or merge join.
  • Cache synergy – Frequently accessed index pages become hot spots in the buffer pool, reducing disk reads for repeated queries.
  • Slow‑query diagnostics – Execution plans that show “Using where; Using filesort” often indicate missing or mismatched indexes.
  • Maintenance – Periodic REINDEX, OPTIMIZE TABLE, or partition‑wise index rebuilds keep fragmentation low and keep the index depth shallow.

Practical Checklist for DBAs and Developers

  • Refresh statistics regularly (ANALYZE TABLE, VACUUM ANALYZE).
  • Monitor hit ratios via information_schema.statistics, pg_stat_user_indexes, or vendor‑specific DMVs.
  • Inspect plans with EXPLAIN or EXPLAIN ANALYZE; look for “Using index” (covering) vs. “Using temporary” (inefficient).
  • Combine with partitioning – Create local indexes on partition keys to shrink the search space.
  • Avoid over‑indexing – Every new index should be justified by a concrete read‑heavy workload.
  • Schedule maintenance – Rebuild heavily fragmented indexes during low‑traffic windows.

Conclusion

Indexes are the cornerstone of relational‑database performance. By providing a key‑to‑row mapping, they transform costly linear scans into logarithmic lookups, enable fast ordering, and enforce uniqueness. Understanding the different index structures, their strengths and trade‑offs, and the principles for selecting and maintaining them equips developers and DBAs to design schemas that scale gracefully. Mastery of indexes paves the way for deeper topics such as query‑plan tuning, cache coordination, and systematic slow‑query remediation—essential skills for any performance‑focused data professional.