Recipe · 15 minutes · Works on: Cockroach Continuum, CockroachDB Cloud, self-hosted · Verified on: CockroachDB v26.4.1, Dec 1, 2026
What you’ll fix
A “latest orders for this customer” query that gets slower as the table grows. The plan shows aFULL SCAN: to return 10 rows, CockroachDB reads all 100,000. You’ll find the query, read its plan, and add one secondary index so the same query reads 10 rows.
Full table scans are the most common reason a query that was fast in development is slow in production. They also make transaction contention worse, because a query that reads more rows conflicts with more writes.
Start a cluster
- Cockroach Continuum
- Local (self-hosted)
Create a free cluster by following the Continuum quickstart, then open a SQL shell with the connection string from the Console.
Create the problem
Anorders table keyed by a random UUID, with 100,000 rows across 5,000 customers:
Find it
Read the plan. PutEXPLAIN in front of the query:
spans: FULL SCAN on the primary index, 100% of the table, and an index recommendation from the optimizer.
Find it across the workload. You usually don’t know which query is the problem. SHOW FULL TABLE SCANS lists the statement fingerprints whose plans used a full scan. Run the query itself at least once first: EXPLAIN doesn’t count, and a statement appears in the list about a second after it runs.
Fix it
Create the index the optimizer recommended. It matches the query: the filter column first, then the sort column, with the returned columns stored so the query never goes back to the primary index.created_at is in the key, the rows are already in order and the query can stop after 10. Always read a recommendation before applying it: it covers the one query, not the write cost to the rest of your workload.
Run EXPLAIN ANALYZE to compare what actually happened:
CREATE INDEX runs online. Reads and writes continue while the index is built in the background.
How it fails
Trade-offs and when not to use it
- Every index costs writes. Each
INSERT,UPDATE, andDELETEalso writes to every secondary index. Check for indexes nothing reads with the Schema Insights tab, or withinformation_schema.crdb_index_usage_statistics, and drop them. STORINGtrades storage for reads. Stored columns are copied into the index. Store only what the query returns.- Sequential keys can create a hotspot. An index whose leading column only grows, such as a timestamp, sends all new writes to one range. Lead with a column that spreads out, or use a hash-sharded index. See Understand hotspots.
On Cockroach Continuum with Cockroach Aegis (private preview): Aegis watches the workload and proposes index recommendations with their expected effect. See Review and apply an index recommendation.
Give it to your agent
Go deeper
- Secondary indexes and index best practices
EXPLAINandEXPLAIN ANALYZE- Statement tuning with
EXPLAIN - Performance Tuning Recipes, where this recipe came from
Practice query tuning in Cockroach University
Free developer path with hands-on browser labs.

