User Agents

Web browsers and everyday web tech, explained

MySQL Query Cache Guide: Strategy & Tips

· 6 min read · Cookies and Cache

In this article
  1. Background: Why the Query Cache Existed
  2. How the Query Cache Works
  3. Choosing the Right Configuration
  4. Common Problems and Misconceptions
  5. Alternatives to the Query Cache
  6. Costs and Performance Impact
  7. How to Check if the Query Cache Is Helping
  8. Frequently Asked Questions

The MySQL query cache is a server-side feature that stores the text of a SELECT statement along with its result set, so that if the identical statement is executed again, MySQL can return the cached result without re-executing the query. It is most effective for read-heavy workloads with many repeated, identical queries, but it is disabled by default in MySQL 5.7.20 and removed entirely in MySQL 8.0. This guide explains how the query cache works, when to use it, how to configure it, and what to use instead.

Background: Why the Query Cache Existed

Before MySQL 8.0, the query cache was a built-in optimization aimed at reducing the cost of executing the same SELECT statement repeatedly. The idea was simple: if you run the exact same query text (byte-for-byte identical, including whitespace and case) and the underlying tables have not changed, MySQL can serve the result from memory. This was especially appealing for busy web applications where many users hit the same pages and issue the same queries.

However, the query cache had a serious flaw: every write to a table invalidates all cached entries for that table. On a system with frequent UPDATE, INSERT, or DELETE operations, the cache becomes a bottleneck because invalidation happens constantly, and the cache maintenance itself consumes CPU and memory. This is why MySQL 8.0 removed it entirely, and why many production systems disabled it even before that.

How the Query Cache Works

When enabled, the query cache stores the exact query string and the result rows. The matching is case-sensitive and whitespace-sensitive, so SELECT * FROM users and select * from users are considered different. Also, the query must be a SELECT – not a SELECT with functions like NOW() or RAND(), because those are non-deterministic. The cache also ignores queries that use user variables, temporary tables, or refer to system tables.

On a cache hit, MySQL returns the result without parsing the query again, which saves CPU. On a miss, it executes the query and stores the result if the result fits within the configured query_cache_limit (default 1 MB). The total memory for the cache is set by query_cache_size (default 0, meaning disabled).

Choosing the Right Configuration

If you are on MySQL 5.7 or earlier and want to use the query cache, you need to set three variables:

  • query_cache_type – can be 0 (OFF), 1 (ON), or 2 (DEMAND). In DEMAND mode, only queries that include SQL_CACHE hint are cached.
  • query_cache_size – the amount of memory to allocate. Set to 0 to disable, or a value like 64M or 128M.
  • query_cache_limit – the maximum result size that will be cached. Larger results are not cached.

A common starting point is query_cache_type=1 and query_cache_size=64M. But monitor the Qcache_hits and Qcache_inserts status variables. If you see a low hit rate (hits / (hits+inserts)) or high Qcache_lowmem_prunes, the cache is not helping and may be hurting. In that case, set query_cache_size=0 to disable it.

Common Problems and Misconceptions

The biggest misconception is that the query cache is a universal speed booster. In reality, it can slow down write-heavy workloads. Every write to a table invalidates all cached queries for that table, and the invalidation process itself requires locking. On a busy e-commerce site with constant order updates, the query cache can cause severe contention.

Another problem is memory fragmentation. The query cache allocates memory in blocks, and over time, fragmentation can lead to wasted memory and even out-of-memory errors. MySQL tries to defragment, but it is not always effective.

Also, the query cache does not share across MySQL instances. If you have a replication cluster, each node has its own cache, and the cache on the replica is useless because reads typically go to the primary.

Alternatives to the Query Cache

Since MySQL 8.0 removed the query cache, you need other strategies for caching query results:

  • Application-level caching: Use Memcached or Redis to store result sets. This gives you full control over invalidation and is much more flexible.
  • MySQL's InnoDB buffer pool: If your data fits in memory, the buffer pool can serve repeated queries very quickly because the data pages are already in memory. This is not the same as caching the result, but it often makes the query cache unnecessary.
  • Proxy caching: Tools like ProxySQL can cache query results at the proxy level, with more sophisticated invalidation rules.
  • Materialized views or summary tables: Precompute expensive aggregations and update them periodically.

For most modern applications, the best approach is to tune the InnoDB buffer pool size and use an external cache for truly repeated queries.

Costs and Performance Impact

Even when the query cache is enabled, there is overhead on every query. MySQL must check the cache on every SELECT, which involves a hash lookup. This overhead is small, but on a system with very few repeated queries, it is wasted. Additionally, the cache consumes memory that could otherwise be used by the InnoDB buffer pool. If you allocate 128M to the query cache, that memory is not available for data pages.

In a read-only workload with many identical queries, the query cache can reduce response time dramatically. But in a mixed workload, the cost of invalidation often outweighs the benefit. The MySQL manual itself noted that the query cache can be a bottleneck on multi-core systems because of global mutexes.

How to Check if the Query Cache Is Helping

You can run SHOW STATUS LIKE 'Qcache%'; to see the cache statistics. Key variables:

  • Qcache_hits – number of times a query was served from cache.
  • Qcache_inserts – number of queries that were inserted into the cache.
  • Qcache_lowmem_prunes – number of entries removed due to memory pressure.
  • Qcache_free_memory – free memory in the cache.

If Qcache_hits is much higher than Qcache_inserts, the cache is useful. If Qcache_lowmem_prunes is high, increase the size or reduce the limit. If Qcache_free_memory is consistently low, you may need more memory or a smaller cache.

Frequently Asked Questions

Is the MySQL query cache still available in MySQL 8.0?

No. The query cache was removed in MySQL 8.0. If you upgrade from 5.7 to 8.0, you must disable it before upgrading because the server will ignore the variables and may log warnings.

Can I use the query cache with InnoDB tables?

Yes, the query cache works with InnoDB tables, but it is not recommended for write-heavy InnoDB workloads because every write invalidates the cached entries. InnoDB's buffer pool is usually a better investment.

Does the query cache work with prepared statements?

Prepared statements are not cached by the query cache because the query text is not sent in the same way. The cache only works with literal SQL statements sent directly to the server.

What is the difference between query_cache_type=1 and type=2?

Type 1 (ON) caches all eligible SELECT queries automatically. Type 2 (DEMAND) only caches queries that include the SQL_CACHE hint, giving you explicit control over which queries are cached.

How do I disable the query cache if I don't want it?

Set query_cache_size=0 and query_cache_type=0 in the MySQL configuration file, then restart the server. In MySQL 5.7.20 and later, the query cache is disabled by default.