Data6 min read10 interview questions

Database indexes: why queries get fast

What an index is, when it helps, and what it costs.

In one sentenceAn index is a sorted shortcut that lets the database jump to the rows you want instead of reading the whole table.

Think of it like thisIt is the index at the back of a book: you look up a word and go straight to the page, instead of reading every page.

The big picture

Tap any step to see where to use it, how to use it and an example result.

How it speeds things up

Without an index, the database reads every row. This is fine for hundreds of rows and painfully slow for millions. An index keeps a sorted copy of one or more columns, so finding a value takes a few steps.

What it costs

Every index takes space and must be updated whenever data is added or changed, so writes get slower. Index the columns you search, filter or join on often, not everything.

Check that it is used

Ask the database to explain a query. If it says it scans the whole table when you expected an index, the query or the index needs changing. Searches that start with a wildcard, like %term, often cannot use a normal index.

Common mistakes

  • Adding an index to every column.
  • Not indexing the columns used in joins.
  • Guessing instead of checking the query plan.

Quick recap

  • An index is a sorted shortcut
  • It speeds up reading and slows down writing
  • Index columns you filter and join on
  • Use the explain command to check

Try it yourself

Create a table with 100,000 rows in SQLite, time a search on an unindexed column, add an index, and time it again.

Top 10 interview questions

Ordered from basic to advanced. Try answering out loud first, then open the answer.

  1. What is a database index?Basic

    A separate structure that lets the database find rows quickly without reading the whole table, like the index at the back of a book.

  2. Why do indexes make queries faster?Basic

    Instead of a full table scan, the database searches the sorted index, usually a B-tree, and jumps straight to the matching rows.

  3. What is the cost of adding an index?Basic

    It uses extra storage and slows down inserts, updates and deletes, because the index must be kept up to date as well.

  4. Which columns are good candidates for an index?Basic

    Columns used often in WHERE filters, JOIN conditions and ORDER BY, especially when they have many different values.

  5. What is a composite index and why does column order matter?Intermediate

    An index on several columns, such as (country, city). It helps queries that filter by the leftmost columns, so it can serve country, or country and city, but not city alone.

  6. What is a unique index, and how does it relate to a primary key?Intermediate

    A unique index makes the database reject duplicate values. A primary key is unique and not null, and is normally backed by a unique index.

  7. What is a covering index?Intermediate

    An index that holds all the columns a query needs, so the database can answer from the index alone without visiting the table.

  8. When does the database ignore an index?Intermediate

    When the query wraps the column in a function, uses a leading wildcard like LIKE '%abc', compares different types, or the filter matches a large share of the table so a scan is cheaper.

  9. How do you check whether a query uses an index?Advanced

    Run EXPLAIN (or EXPLAIN ANALYZE) and read the plan. Look for an index scan or seek rather than a sequential or full table scan, and compare estimated and actual row counts.

  10. What is index selectivity, and what are the trade-offs between B-tree and hash indexes?Advanced

    Selectivity is how well an index narrows results; high selectivity (many distinct values) works best. B-trees support ranges and sorting, while hash indexes only support equality lookups.

Back to Learn Hub

Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.