Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Databases, SQL & BIDatabase conceptMiddleCommonTheory

What does a database index improve, and what costs or risks does it introduce?

Answer

An index provides an additional structure that can locate or order selected rows faster than scanning the whole table. It consumes storage and adds work to inserts, updates and deletes, and the optimizer may correctly ignore it when a query returns much of the table. Test representative data volume and distribution, query predicates, ordering, write cost, uniqueness requirements, stale statistics and the measured plan instead of merely checking that an index exists.

Strong answer includes

  • balances read benefit against write and storage cost
  • recognizes that optimizers may choose a scan
  • uses representative data and measured plans

Practical example

Index a real query shape and measure it
Dialect · PostgreSQLRuntime · static example

EXPLAIN (ANALYZE, BUFFERS) is PostgreSQL-specific; a PostgreSQL execution engine is not wired into the browser yet.

CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at DESC);

EXPLAIN (ANALYZE, BUFFERS)
SELECT id, status, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;

The index matches the equality predicate and requested ordering. The execution plan proves whether the optimizer actually uses it and whether the measured read benefit justifies its storage and write cost.

Expected result: A plan that can retrieve the customer's newest rows efficiently on representative data.

#database#sql#indexes#performance

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recoveryGoogle SRE booksGoogle · Reliability, SLOs, monitoring, incidents and resilience