Databases & StorageIntermediate16 min read3 questions

Database Indexes and Query Performance

Why one query returns in 2 ms and an identical-looking one takes 40 seconds. Indexes from first principles, and the storage-engine choice that decides whether your database prefers reads or writes.

Covers: B-trees vs LSM trees, composite indexes, covering indexes, selectivity, EXPLAIN, write amplification

Almost every 'the database is slow' incident is a missing or unusable index. And almost every 'why is our write throughput so bad' incident is too many indexes. Understanding the mechanism - not just the advice - is what lets you reason about a schema you have never seen.

Filter
0/3 mastered
Intermediateindexesb-treeperformanceAsked at Amazon, Stripe

30-second answer

An index is a separate sorted data structure - usually a B+ tree - mapping column values to row locations. It turns an O(n) table scan into an O(log n) tree descent, typically three or four disk pages instead of millions. Writes slow down because every insert, update or delete must also update every index on that table, each of which is an additional random write plus possible page splits. So indexes trade write throughput and disk space for read speed, and a table with twelve indexes can be several times slower to write than the same table with two.

Advancedstorage enginelsmb-treeAsked at Netflix, Discord

30-second answer

A B-tree updates data in place: writes are random I/O but reads are a single predictable tree descent. An LSM-tree buffers writes in memory, flushes them as sorted immutable files, and merges those files in the background - so writes are sequential and fast, but a read may have to check several files, making reads slower and less predictable. B-trees power Postgres, MySQL InnoDB and most relational systems and suit read-heavy or read-modify-write workloads. LSM-trees power Cassandra, RocksDB, LevelDB, ScyllaDB and HBase and suit write-heavy, append-oriented workloads such as time series, event logs and metrics.

AdvancedperformancedebuggingsqlAsked at Stripe, Shopify

30-second answer

Start with EXPLAIN ANALYZE and compare the planner's estimated rows with the actual rows - a large divergence means stale statistics and a bad plan. Then check the four usual causes: data growth crossing a threshold that flipped the plan from index scan to sequential scan, stale or missing statistics after a bulk load, lock contention or long-running transactions blocking progress, and a change in parameter values causing parameter-sniffing to reuse a plan that suits a different value. Confirm with the plan, not with intuition.

Showing 3 of 3 questions for database-indexes-and-query-performance.

Check your understanding

4 questions · no sign-up, nothing stored

0/4 answered
Question 1

1.Given `CREATE INDEX idx ON t (a, b, c)`, which query can NOT use the index for seeking?

Question 2

2.Why is a random UUIDv4 primary key bad for write throughput?

Question 3

3.Which workload is the best fit for an LSM-tree storage engine?

Question 4

4.EXPLAIN ANALYZE shows `rows=1` estimated but `rows=1,240,000` actual. What is the most likely cause?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Index a real schema and prove the improvement

Take a table with at least a million rows (generate one if needed) and optimise a genuinely slow query end to end.

Requirements

  • Write a query that takes over one second without an index and capture its EXPLAIN ANALYZE.
  • Design one composite index that serves it, and justify the column order.
  • Re-run and record the new plan, noting the change in rows examined and buffers read.
  • Measure the insert-throughput cost of the new index with a bulk-insert benchmark.
  • State the read/write trade-off you just made, with numbers.

Stretch goals

  • Convert it to a covering index and measure the further gain.
  • Add a partial index for a low-selectivity filter and compare index size.