> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Result Cache

> How to enable and use the FE-side SQL Result Cache in the database, which answers a repeated query by replaying the result of a previous identical query instead of planning and executing it again.

The SQL result cache lives on the FE and holds the **finished result** of a query. When an identical query arrives again, the database replays the stored MySQL result packets and skips analysis, planning, and execution entirely.

It is unrelated to the two BE-side caches:

| Cache | Where | What it holds |
| - | - | - |
| SQL result cache | FE | the whole result of a query, as the bytes that were sent |
| [Query cache](/documents/4.2/using_starrocks/caching/query_cache.md) | BE | intermediate results of local aggregations |
| [Data cache](/documents/4.2/using_starrocks/caching/block_cache.md) | BE | data blocks read from remote storage |

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:

```sql theme={null}
ADMIN SET FRONTEND CONFIG ("enable_sql_result_cache" = "true");
```

There is no session-level switch: a client cannot turn the cache on or off for its own connection.

## 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 (`SELECT` and `select` match); identifiers and literals are taken as written, so `WHERE s = 'a'` and `WHERE 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.

Privileges are deliberately *not* part of the key. They are re-checked on every hit instead, so revoking access takes effect immediately rather than when an entry expires.

## 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 `@x` or `@@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 `SELECT`s 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 than `sql_result_cache_entry_max_bytes`, results with more rows than `sql_result_cache_entry_max_rows`, and queries that finished faster than `sql_result_cache_min_query_duration_ms`.

Internal work — statistics collection, metadata queries, materialized view refresh — is never cached and never counted in the cache metrics.

## 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 `SELECT` privilege on any of the relations no longer confirmable.

Nothing needs to be invalidated explicitly, and there is no background invalidation: an entry that is never looked up again simply occupies memory until it is evicted.

## 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 with `ResultCacheHit=true`, `ScanRows=0`, and no CPU or memory cost recorded, because nothing was scanned.

Five FE metrics are exposed at `/metrics`:

| Metric | Meaning |
| - | - |
| `starrocks_fe_sql_result_cache_hit_total` | queries answered from the cache |
| `starrocks_fe_sql_result_cache_miss_total` | queries that consulted the cache and were not answered from it, including rejection by warehouse privileges or the text SQL blacklist |
| `starrocks_fe_sql_result_cache_entry_count` | entries currently resident |
| `starrocks_fe_sql_result_cache_used_bytes` | bytes currently held, by the same measure as the budget |
| `starrocks_fe_sql_result_cache_avoided_exec_millis_total` | sum of the original durations of every replayed query |

`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, and `LIMIT` can select different rows across executions. Likewise, `group_concat()` without an aggregate `ORDER BY` can concatenate values in different orders, and floating-point `SUM()` or `AVG()` 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 their `pipeline_dop` or `query_mem_limit` differs, because these settings are excluded from the key. Use an `ORDER BY` that fully determines the order when stable row selection or concatenation order is required; caching does not make an otherwise unspecified result deterministic.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.