This guide helps you identify and resolve performance issues in CockroachDB. Learn how to diagnose slow queries, high latency, resource bottlenecks, and other common problems.
-- Problem: Full table scanEXPLAIN SELECT * FROM orders WHERE status = 'pending';-- Shows: FULL SCAN-- Solution: Create indexCREATE INDEX idx_status ON orders (status);
Transaction conflicts occur when concurrent transactions access the same rows. Reduce contention by:
Shortening transaction duration
Using SELECT FOR UPDATE
Avoiding hot spots
Batching updates
-- Problem: Lost updates from concurrent modificationsBEGIN; SELECT balance FROM accounts WHERE id = 'acc123'; -- Application logic UPDATE accounts SET balance = 500 WHERE id = 'acc123';COMMIT;-- Solution: Lock the rowBEGIN; SELECT balance FROM accounts WHERE id = 'acc123' FOR UPDATE; UPDATE accounts SET balance = 500 WHERE id = 'acc123';COMMIT;
-- Problem: Sequential primary key creates hotspotCREATE TABLE events ( id INT PRIMARY KEY, data JSONB);-- All writes go to same range-- Solution: Use hash-sharded keyCREATE TABLE events ( id INT PRIMARY KEY USING HASH WITH (bucket_count = 16), data JSONB);
-- Problem: Large result setSELECT * FROM orders; -- Returns millions of rows-- Solution: Paginate resultsSELECT * FROM orders ORDER BY id LIMIT 1000 OFFSET 0;-- Problem: Large joinSELECT o.*, c.* FROM orders o CROSS JOIN customers c;-- Solution: Add filterSELECT o.*, c.* FROM orders oJOIN customers c ON o.customer_id = c.idWHERE o.created_at > NOW() - INTERVAL '1 day';
-- Reduce cross-region latency for read-heavy workloadsSET TRANSACTION AS OF SYSTEM TIME follower_read_timestamp();SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '7 days';
-- Node statusSELECT node_id, address, build_tag, started_at, updated_at FROM crdb_internal.gossip_liveness;-- Range healthSELECT COUNT(*) AS total_ranges, SUM(CASE WHEN unavailable_replicas > 0 THEN 1 ELSE 0 END) AS unavailable, SUM(CASE WHEN underreplicated_replicas > 0 THEN 1 ELSE 0 END) AS underreplicatedFROM crdb_internal.ranges_no_leases;
# Node statuscockroach node status --host=<node-address># Range statuscockroach node ranges --host=<node-address># Debug zip for supportcockroach debug zip /tmp/debug.zip --host=<node-address>