Why is this Postgres query slow?

Prompt

Here is a slow Postgres query and its EXPLAIN ANALYZE output. Explain in plain English why it is slow, propose the single most effective index (as CREATE INDEX), and say how you would confirm it helped.

{query}

{explain_output}

Sample output

Example run: query = SELECT * FROM orders WHERE customer_id = 42 AND created_at > now() - interval '30 days' ORDER BY created_at DESC LIMIT 20; explain_output = Seq Scan on orders, Rows Removed by Filter: 2398160, followed by a Sort on created_at

Why it's slow: Postgres reads every row in orders (a sequential scan over ~2.4M rows), throws away all but a few thousand, sorts what's left, then returns 20. Almost all the time goes to scanning rows that don't belong to customer 42.

Index:
CREATE INDEX CONCURRENTLY orders_customer_created_idx ON orders (customer_id, created_at DESC);

It matches the filter (equality on customer_id first, then the created_at range) and the ORDER BY, so Postgres can jump straight to customer 42's newest orders and stop after 20 rows with no sort.

Confirm: run EXPLAIN (ANALYZE, BUFFERS) again. You should see an Index Scan using orders_customer_created_idx, no Sort node, and far fewer buffers read.