Home›Learn›Queries that look fine and are not
Course · Inside one serverLesson 10 of 330 read
Queries · for developers

Queries that look fine and are not

Why is my query slow when it has an index?

You will learn: four query shapes behind many slow pages: column order in an index, covering queries, OFFSET paging and N+1, each measured on PostgreSQL, MySQL and MongoDB.

Every number on this page comes from Notebook 5, run 2026-11-18 04:27 UTC on a table of 1,000,000 orders with an index on (customer_id, created_at). The same shape behaved the same way in all three engines, which is the point: these are not engine quirks, they follow from how an index is sorted.

Shape 1

Why is my query slow when the index exists?

The index is on (customer_id, created_at). It is sorted by customer_id first, then by created_at inside each customer. A query that gives both columns narrows to one customer, then reads a range. A query that gives only created_at cannot use that order: the rows for one day are spread across every customer.

Why column order matters

41Jan41Mar41Jun41Aug41Nov42Jan42Mar42Jun42Aug42Nov43Jan43Mar43Jun43Aug43NovIndex on (customer_id, created_at): entries sorted by customer first, then datecustomer_id = 42 AND created_at >= Jun: one contiguous range, read in one passcreated_at >= Nov alone: scattered across every customer, so the engine scans instead
CREATE INDEX ON orders (customer_id, created_at);
-- uses the index
SELECT * FROM orders WHERE customer_id = 42 AND created_at >= '2026-06-01';
-- does not
SELECT * FROM orders WHERE created_at >= '2026-11-17';

PostgreSQL 18

Both columns: Bitmap Index Scan, 48 pages, 0.10 ms.
Second column only: Seq Scan, 7,353 pages, 39 ms.

MySQL 8.4

Both columns: index range scan, 44 rows, 0.14 ms.
Second column only: table scan, 1,000,000 rows read, 224 ms.

MongoDB 8.0

Both fields: IXSCAN, 44 keys, 1 ms.
Second field only: COLLSCAN, 1,000,000 documents, 436 ms.

Rows read: both index columns against the second column alone, log scale

Index on (customer_id, created_at). Measured in Notebook 5.

WHERE created_at >= ... onlyWHERE customer_id = 42 AND created_at >= ...
1101001,00010,000100,0001,000,000PostgreSQLPostgreSQL, Second column only: 1,000,0001,000,000PostgreSQL, Both columns, in order: 4444MySQLMySQL, Second column only: 1,000,0001,000,000MySQL, Both columns, in order: 4444MongoDBMongoDB, Second column only: 1,000,0001,000,000MongoDB, Both columns, in order: 4444

What I do: put the column that is always filtered with equality first, and the range column after it. If a query filters on the second column alone, it needs its own index. The same rule is called ESR (equality, sort, range) in MongoDB.

Shape 2

Can a query skip the table entirely?

If every column the query needs is already in the index, the engine can answer from the index and never read the table. PostgreSQL calls it an index-only scan, MySQL a covering index, MongoDB a covered query.

SELECT customer_id, created_at FROM orders WHERE customer_id = 42;
// MongoDB: project only indexed fields, and leave out _id
db.orders.find({customer_id: 42}, {_id: 0, customer_id: 1, created_at: 1})

PostgreSQL 18

Index Only Scan, Heap Fetches: 0, 5 pages, 0.06 ms. It depends on the visibility map, so a table that VACUUM has not caught up with will still fetch heap pages.

MySQL 8.4

Covering index lookup, 100 rows, 0.02 ms.

MongoDB 8.0

PROJECTION_COVERED, 100 keys, 0 documents examined.

What I do: for a hot query that reads a few columns, I add those columns to the index (INCLUDE in PostgreSQL, or simply more columns at the end), and check the plan says covering before I call it done.

Shape 3

Why does page 25,000 take a hundred times longer than page 1?

OFFSET does not jump. To skip 500,000 rows, the engine reads them and throws them away. Paging by key remembers where the last page ended, and starts there.

-- reads 500,020 rows to return 20
SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 500000;
-- reads 20
SELECT * FROM orders WHERE id > 500000 ORDER BY id LIMIT 20;

PostgreSQL 18

OFFSET: 500,020 rows through the index, 5,046 pages, 64 ms.
By key: 20 rows, 4 pages, 0.06 ms.

MySQL 8.4

OFFSET: 500,020 rows read, 99 ms.
By key: 20 rows read, 0.26 ms.

MongoDB 8.0

skip: 500,020 keys examined, 231 ms.
By key: 20 keys, under 1 ms.

Rows read to return page 25,001 (20 rows), log scale

OFFSET 500000 against WHERE id > 500000. Measured in Notebook 5 on 1,000,000 orders.

OFFSET 500000 / skip(500000)WHERE id > 500000
1101001,00010,000100,0001,000,000PostgreSQLPostgreSQL, OFFSET or skip: 500,020500,020PostgreSQL, By key: 2020MySQLMySQL, OFFSET or skip: 500,020500,020MySQL, By key: 2020MongoDBMongoDB, OFFSET or skip: 500,020500,020MongoDB, By key: 2020

What I do: page by key (also called keyset or seek paging). The API returns the last id or the last (created_at, id) pair, and the next request starts after it. Jumping to an arbitrary page number is the one thing it cannot do, and few users need that.

Shape 4

Why is a fast query making the page slow?

An ORM loads 100 customers, then runs one more query per customer for their latest order. Each query is fast. The page is slow because it pays 101 network round trips.

-- 100 round trips
for id in customer_ids: SELECT max(created_at) FROM orders WHERE customer_id = %s
-- 1 round trip
SELECT customer_id, max(created_at) FROM orders
WHERE customer_id = ANY(%s) GROUP BY customer_id;

PostgreSQL 18

100 queries: 20.1 ms. One query: 1.6 ms. Measured from a client on the same machine. Across a network, each round trip costs more, and the gap grows.

MySQL 8.4

Same pattern: one query with IN (...) or a JOIN instead of one per row. Not timed in this run.

MongoDB 8.0

Same pattern: one find with $in, or one aggregation with $group. Not timed in this run.

What I do: look at the query count per request, not just the slowest query. In the database, N+1 shows up as one statement with a huge call count and a tiny mean time in pg_stat_statements or performance_schema.

I learned most of this from Markus Winand's Use The Index, Luke, which goes much deeper on indexing for developers. This page adds the same experiments on all three engines, with measured output.

Connected

Where this shows up

The same mechanism, indexes and plans, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. The index is on (customer_id, created_at). A query filters on created_at alone. What happened in all three engines?

2. How many rows does OFFSET 500000 LIMIT 20 read?

3. How does PostgreSQL show that a query never touched the table?

4. What makes N+1 queries slow when each query is fast?

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.