-
Step 1 of 5Measure
Start from the real queries
Every search, account page and quote request becomes a database query. I start by listing the queries the API really sends and reading their plans: EXPLAIN ANALYZE in PostgreSQL, EXPLAIN in MySQL, explain() in MongoDB. No guessing which fields need indexes.
-
Step 2 of 5Edge case
No matching index: read everything
When no index matches a query's filter and sort, the planner reads everything (a sequential scan in SQL, a COLLSCAN in MongoDB) to find a few rows, then sorts them in memory. Fine on a small catalogue, slower every month it grows.
-
Step 3 of 5Fix
One compound index per query shape
Each frequent query gets a composite index: equality fields first, then the sort field, then range filters. The same rule works in PostgreSQL, MySQL and MongoDB. The planner now walks the index and reads only the matching rows, already in order.
-
Step 4 of 5Design
Shape documents around reads
Schema design decides how many reads a page needs. In SQL that means the right joins and covering indexes; in MongoDB, embedding data that's always shown together and referencing data that grows or is shared. Either way, pages need fewer round trips.
-
Step 5 of 5Result
Queries 30% faster
On the catalogue platform at Cocoon, designing the MongoDB schemas and indexing strategies this way improved query performance by 30%. After that, reading the query plan belongs in code review, so a missing index shows up before it ships.
Tip: the ← and → keys work too.