Because a hit skips execution, the saving is the whole query rather than part of it — and for the same reason a hit produces no query profile and no scan statistics.
Enable the cache
The cache is off by default, and turning it on is an administrator action:How a query is matched
The key of an entry is derived from three things, all taken from the query as it was submitted:- The statement text, as tokens. Keyword case and whitespace do not matter (
SELECTandselectmatch); identifiers and literals are taken as written, soWHERE s = 'a'andWHERE s = 'A'are different queries. - Who is asking — the user identity and the active roles. Two users never share an entry.
- The session settings that can change a result — every session variable that is not at its default value, except a short list of execution settings (such as timeouts, queueing, degree of parallelism, and memory limits). See the result reproducibility caveat under Precautions.
What is not cached
A statement is refused if anything about it cannot be modelled. The main groups:- Non-deterministic or session-dependent functions —
rand(),uuid(),now(),curdate(),current_user(),connection_id(),host_name(),version(), and any zero-argument function not known to be constant. - User-defined functions, including UDAFs and table functions: replacing the implementation under the same name would leave a stale entry.
- References to variables, such as
@xor@@sql_mode. - Relations that bind to something other than a logical result —
PARTITION (...),TEMPORARY PARTITION (...),TABLET (...),REPLICA (...),SAMPLE (...),FOR VERSION/AS OF,CHANGES, bookmarks, and the[_META_]and[_CACHE_STATS_]hints. - Anything outside the internal catalog. An external table has no version, partition set, or schema identity that the cache can re-check.
- Materialized view scans, whether the view was named or substituted by the optimizer.
- Statements that are not plain
SELECTs over the MySQL text protocol: prepared statements, HTTP requests, Arrow Flight SQL,SELECT ... INTO OUTFILE, statements inside a transaction, and statements forwarded to the leader. - Queries that asked for an execution artefact —
enable_profile = true, or a query dump — because both are produced by executing and a replay executes nothing. - Statements longer than
sql_result_cache_max_sql_length, results larger thansql_result_cache_entry_max_bytes, results with more rows thansql_result_cache_entry_max_rows, and queries that finished faster thansql_result_cache_min_query_duration_ms.
When an entry stops being used
Every entry records what its result depended on, and those dependencies are re-checked against current metadata on every lookup. An entry is refused when any of the following has happened since it was written:- a load into a partition the query read — a load into a partition the query did not read leaves the entry usable;
- a partition added, dropped, or replaced;
- a schema change, including a change to a generated column’s expression;
- a rollup or synchronous materialized view added or dropped;
- a view the query reads redefined;
- a row access policy or column masking policy changed;
- the table dropped, recreated, or swapped;
- the user’s
SELECTprivilege on any of the relations no longer confirmable.
Memory
sql_result_cache_total_max_bytes bounds what the cache holds. An entry’s weight is its whole footprint — the result bytes plus the dependency record it retains — not only the payload.
Two things to know:
-
The budget is fixed when the FE starts. A new value takes effect at the next restart.
An entry nobody reads is dropped after
sql_result_cache_idle_timeout_sec(600 by default). That clock is reset by every read, so it bounds idle memory rather than staleness: a query that keeps being asked keeps its entry, and freshness is re-checked on every lookup either way. - Turning the cache off does not release memory. With the cache off nothing is served and nothing is evicted; resident entries are released when the process restarts.
Observability
A query answered from the cache appears in the audit log withResultCacheHit=true, ScanRows=0, and no CPU or memory cost recorded, because nothing was scanned.
Five FE metrics are exposed at /metrics:
avoided_exec_millis_total is an estimate of work avoided, not a measured saving: it does not subtract what the hit path itself costs, and it assumes a re-execution would take as long as the execution that was recorded.
Precautions
- A hit produces no query profile, so a session that asks for one is not served from the cache at all. With
enable_profile = true, or with a query dump requested, the cache is neither consulted nor populated, and every such query executes and produces the artefact you asked for. - A SQL blacklist rule matches served queries, a digest blacklist rule does not. The text rule is checked on every lookup, so it stops a cached query as soon as it is added. A digest is computed from the analyzed statement, which a hit never produces, so a digest rule only takes effect on a warmed query shape once its entry is gone — through a dependency change or through eviction. Use a text rule, or turn the cache off, to stop a warmed shape at once.
- A hit reuses one execution’s result, including any unspecified ordering or floating-point variation. Without
ORDER BY, row order is unspecified, andLIMITcan select different rows across executions. Likewise,group_concat()without an aggregateORDER BYcan concatenate values in different orders, and floating-pointSUM()orAVG()can vary with the order of partial aggregation. The cache repeatedly returns the result captured from one execution. Sessions with the same identity and other matching key components can share that result even when theirpipeline_doporquery_mem_limitdiffers, because these settings are excluded from the key. Use anORDER BYthat fully determines the order when stable row selection or concatenation order is required; caching does not make an otherwise unspecified result deterministic.