Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

Cassandra Slow Queries

Slow queries manifest as high latency, timeouts, or degraded application performance. This playbook helps identify and resolve query performance issues.


  • High read/write latencies in nodetool proxyhistograms
  • Query timeouts from client applications
  • Specific queries consistently slow
  • “Slow query” warnings in logs
  • User-reported application slowness

Terminal window
# Coordinator latencies
nodetool proxyhistograms
# Per-table latencies
nodetool tablehistograms my_keyspace my_table

What to look for:

  • p99 latency > 100ms for reads
  • p99 latency > 50ms for writes
  • Large gap between p50 and p99 (inconsistent performance)
cassandra.yaml
slow_query_log_timeout_in_ms: 500

Then check logs:

Terminal window
grep "slow query" /var/log/cassandra/debug.log | tail -50
TRACING ON;
SELECT * FROM my_table WHERE ...;
TRACING OFF;

What to look for in trace:

  • Time spent in each phase
  • Number of SSTables read
  • Tombstones scanned
  • Partitions touched
Terminal window
nodetool tablestats my_keyspace.my_table

Problem indicators:

  • High SSTable count (> 20)
  • High tombstones per slice
  • Large partition sizes
  • Low key cache hit rate
Terminal window
# Top partitions by read/write activity
nodetool toppartitions my_keyspace my_table 10000

Problem:

SELECT * FROM users; -- Scans entire cluster

Solution:

-- Add WHERE clause on partition key
SELECT * FROM users WHERE user_id = ?;
-- Or use pagination
SELECT * FROM users LIMIT 100;

Problem:

SELECT * FROM users WHERE email = 'test@example.com' ALLOW FILTERING;

Solution:

-- Create secondary index
CREATE INDEX ON users (email);
-- Or create materialized view
CREATE MATERIALIZED VIEW users_by_email AS
SELECT * FROM users
WHERE email IS NOT NULL AND user_id IS NOT NULL
PRIMARY KEY (email, user_id);

Problem:

SELECT * FROM orders WHERE order_id IN (uuid1, uuid2, ..., uuid100);

Solution:

-- Use async parallel queries from application
-- Or batch into smaller groups
SELECT * FROM orders WHERE order_id IN (uuid1, uuid2, uuid3);

Query Anti-Pattern: Range Queries on Clustering Columns

Section titled “Query Anti-Pattern: Range Queries on Clustering Columns”

Problem:

SELECT * FROM events WHERE user_id = ? AND event_time > '2024-01-01';
-- Scans potentially millions of rows

Solution:

-- Add LIMIT
SELECT * FROM events WHERE user_id = ? AND event_time > '2024-01-01' LIMIT 1000;
-- Or redesign for bounded queries
SELECT * FROM events WHERE user_id = ? AND day = '2024-01-15';

See Large Partition Issues.

Terminal window
# Check partition sizes
nodetool tablestats my_keyspace.my_table | grep partition

See Tombstone Accumulation.

Terminal window
# Check tombstone counts
nodetool tablestats my_keyspace.my_table | grep tombstone
Terminal window
# Check pending compactions
nodetool compactionstats
# If backlog exists
nodetool compact my_keyspace my_table

Infrastructure Issue: Insufficient Resources

Section titled “Infrastructure Issue: Insufficient Resources”
Terminal window
# Check CPU
top -p $(pgrep -f CassandraDaemon)
# Check disk I/O
iostat -x 1 5
# Check thread pools
nodetool tpstats

CheckGoodBadFix
Partition key in WHEREYesNoAdd partition key filter
ALLOW FILTERINGNot usedUsedCreate index or view
IN clause size< 10 values> 100 valuesParallel queries
Result set sizeLIMIT usedNo LIMITAdd LIMIT
Table SSTable count< 20> 50Run compaction
Tombstones per read< 100> 1000Fix data model
Key cache hit rate> 90%< 50%Increase cache

Terminal window
# Check latencies after fix
nodetool tablehistograms my_keyspace my_table
# Trace query again
TRACING ON;
<your query>;
TRACING OFF;

Set up alerts on:

  • p99 read latency > 100ms
  • p99 write latency > 50ms
  • Slow query log entries

  1. Review queries before production - Check execution plans
  2. Monitor query latencies - Alert on degradation
  3. Design data model for queries - Don’t retrofit
  4. Use prepared statements - Reduce parsing overhead
  5. Implement client-side caching - Reduce load for hot data
  6. Run regular compaction - Keep SSTable counts low

CommandPurpose
nodetool proxyhistogramsOverall latencies
nodetool tablehistogramsPer-table latencies
nodetool tablestatsTable health metrics
nodetool toppartitionsIdentify hot partitions
TRACING ON/OFFQuery tracing