Skip to main content
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 a FULL 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

Create a free cluster by following the Continuum quickstart, then open a SQL shell with the connection string from the Console.

Create the problem

An orders table keyed by a random UUID, with 100,000 rows across 5,000 customers:
The application asks for one customer’s 10 most recent orders:

Find it

Read the plan. Put EXPLAIN in front of the query:
Three things to read: 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.
In the Console, the same queries appear on the Insights page as Suboptimal plan insights, with the index recommendation attached: the Cloud Console Insights page or the DB Console Insights page for self-hosted clusters.

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.
Because 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, and DELETE also writes to every secondary index. Check for indexes nothing reads with the Schema Insights tab, or with information_schema.crdb_index_usage_statistics, and drop them.
  • STORING trades 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

Practice query tuning in Cockroach University

Free developer path with hands-on browser labs.