MariaDB lets you enable, disable, or clear the query cache using SQL_CACHE / SQL_NO_CACHE hints, cache variables, and FLUSH commands.
The query cache stores complete result sets for identical SELECT statements. When the same query arrives again, MariaDB can skip execution and return the cached rows instantly, lowering latency and CPU usage for read-heavy workloads.
Use SQL_CACHE
when a query runs often with identical text and the underlying tables change infrequently.Use SQL_NO_CACHE
for volatile tables or ad-hoc queries to avoid useless cache fills and invalidations.
Set query_cache_type
to ON
(every eligible SELECT is cached), DEMAND
(only SQL_CACHE
is cached), or OFF
. Adjust query_cache_size
to allocate memory. Changes need SUPER
privileges.
FLUSH QUERY CACHE
defragments memory while keeping cached results.RESET QUERY CACHE
removes all entries and frees memory immediately—handy after bulk loads into Products
or OrderItems
.
query_cache_size
defines the memory pool; values under 2 MB disable the cache. query_cache_limit
caps the size of a single cached result. query_cache_min_res_unit
controls internal block size and fragmentation.
Probably not. Each INSERT
, UPDATE
, or DELETE
on Orders
or OrderItems
invalidates all cached queries that reference those tables.Heavy DML traffic can erase most performance gains.
Start with query_cache_type = DEMAND
, cache only known hot SELECTs, monitor Qcache_hits
vs Qcache_inserts
, and keep query_cache_size
modest (≤256 MB) to reduce mutex contention.
An analytics dashboard repeatedly fetches yesterday’s revenue. Mark the query with SQL_CACHE
, schedule a nightly RESET QUERY CACHE
after ETL loads, and enjoy millisecond responses during the day.
.
Yes, cached results never replicate. Each replica maintains its own cache, so enabling it does not affect binary logging.
Yes, but the SQL text must be byte-for-byte identical, including whitespace and comments, for the cache to recognize a hit.
Generally no. High write rates and cluster invalidations tend to nullify benefits. Benchmark carefully before enabling.