Part 7: Performance Tuning
Making it fly. Indexes, EXPLAIN, and why you shouldn't scan the whole cluster.
A distributed database is not a magic bullet. If you write bad queries, they will be slow. In fact, they might be slower than on a single node because of network latency.
Indexes are Critical
In a distributed system, a full table scan means visiting every node in the cluster. This is expensive. You MUST use indexes.
CREATE INDEX on users (email);
EXPLAIN ANALYZE
Just like Postgres, CockroachDB has an EXPLAIN command. It tells you exactly how a query will be executed.
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'alice@example.com';
Look for distribution: full. That’s usually bad. You want queries to be routed to specific nodes.
Topology Awareness
CockroachDB is topology-aware. It knows which nodes are close to each other. But you need to help it by designing your schema correctly (see Part 4).
Conclusion
Tuning a distributed database is an art. But the real superpower of CockroachDB is its ability to span the globe.