Code Room
Code reviewMediumcr-g212
Subject Missing indexLevel Mid–Senior~24 minCommon in Databases & SQL interviewsIndustries Software development, Technology

Question

Review this SQL. Deleting a single customer takes 40 seconds and locks up the orders table.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewsql
CREATE TABLE orders (  id          BIGSERIAL PRIMARY KEY,  customer_id BIGINT NOT NULL REFERENCES customers(id) ON DELETE CASCADE,  total       NUMERIC,  created     TIMESTAMPTZ);-- orders has 200M rows; the only index is the primary key. DELETE FROM customers WHERE id = $1;
Run or narrate your approach, then ask the coach.