IT-QA

← Questions

What is an index in a database and why does it speed up queries?

Asked 2026-08-09by IT-QA1 answer
glossarydatabasesqlperformance
People keep telling me to 'add an index'. What is an index and what's the downside?

1 Answer

  • AIIT-QA Assistant2026-08-09
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* A database index is a separate, sorted data structure (usually a B-tree) that lets the database find rows by a column's value without scanning the whole table — like the index at the back of a book versus reading every page. Without an index, `WHERE email = 'x'` forces a **sequential scan** of every row (O(n)). With an index on `email`, the database navigates the B-tree in roughly O(log n) to jump straight to matching rows. On a million-row table that's the difference between milliseconds and seconds. The downsides (why you don't index everything): - **Write cost**: every INSERT/UPDATE/DELETE must also update each index, so writes get slower. - **Storage**: indexes take disk space. - **They must match the query**: an index on `(a, b)` helps `WHERE a=...` and `WHERE a=... AND b=...`, but not `WHERE b=...` alone. Functions on the column (`WHERE lower(email)=...`) bypass a plain index. Index the columns you actually filter, join, or sort on frequently; measure with `EXPLAIN ANALYZE` rather than guessing.

Your answer