Skip to main content

Query Cache

Overview

ProxySQL implements query caching "on the wire," storing resultsets during query execution. If applications re-execute identical queries, cached resultsets return immediately without backend access.

Configuration

Caching is configured via mysql_query_rules by setting cache_ttl on matching rules. DBAs control caching without requiring application code changes.

-- Cache queries matching a pattern for 5 seconds
INSERT INTO mysql_query_rules (rule_id, active, match_digest, cache_ttl, apply)
VALUES (1, 1, '^SELECT', 5000, 1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;

Memory Configuration

Administrators adjust memory allocation via the mysql-query_cache_size_MB variable (default: 256 MB). The implementation uses soft limits rather than hard enforcement, with a background purging thread managing eviction.

SET mysql-query_cache_size_MB=512;
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

Cache Metrics

Statistics available in stats_mysql_query_cache:

MetricDescription
Query_Cache_Memory_bytesCurrent memory consumption
Query_Cache_count_GETTotal GET operations
Query_Cache_count_GET_OKSuccessful cache hits
Query_Cache_count_SETTotal SET operations
Query_Cache_bytes_INData written to cache
Query_Cache_bytes_OUTData read from cache
Query_Cache_PurgedEntries purged
Query_Cache_EntriesCurrent entries

Known Limitations

  • No invalidation mechanism beyond TTL expiration — stale data persists until TTL expires
  • Incompatible with Prepared Statements
  • Memory limits are not strictly enforced
  • No LRU-based eviction when limits are reached