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:
| Metric | Description |
|---|---|
Query_Cache_Memory_bytes | Current memory consumption |
Query_Cache_count_GET | Total GET operations |
Query_Cache_count_GET_OK | Successful cache hits |
Query_Cache_count_SET | Total SET operations |
Query_Cache_bytes_IN | Data written to cache |
Query_Cache_bytes_OUT | Data read from cache |
Query_Cache_Purged | Entries purged |
Query_Cache_Entries | Current 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