mysqldude

Query_cache_type

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SET SESSION query_cache_type = 2 (original)
original post
✗ 1651✗ 1651✗ 1193✗ 1193✗ 1193✗ 1925✗ 1925✗ 1925✗ 1925
SELECT SQL_CACHE ... (original)
original post
✓⚠ 1681✗ 1054✗ 1054✗ 1054✓✓✓✓
SELECT SQL_NO_CACHE ... (original)
original post
✓⚠ 1681⚠ 1681⚠ 1681⚠ 1681✓✓✓✓
SET SESSION query_cache_type = 2 (original) original post
MySQL 5.6✗ 16515.7✗ 16518.0✗ 11938.4✗ 11939✗ 1193
MariaDB 10.6✗ 192510.11✗ 192511.4✗ 192511.8✗ 1925
SELECT SQL_CACHE ... (original) original post
MySQL 5.6✓5.7⚠ 16818.0✗ 10548.4✗ 10549✗ 1054
MariaDB 10.6✓10.11✓11.4✓11.8✓
SELECT SQL_NO_CACHE ... (original) original post
MySQL 5.6✓5.7⚠ 16818.0⚠ 16818.4⚠ 16819⚠ 1681
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (6)
  • SET SESSION query_cache_type = 2 (original) (MySQL 5.6, MySQL 5.7): error 1651: Query cache is disabled; restart the server with query_cache_type=1 to enable it
  • SET SESSION query_cache_type = 2 (original) (MySQL 8.0, MySQL 8.4, MySQL 9): error 1193: Unknown system variable 'query_cache_type'
  • SET SESSION query_cache_type = 2 (original) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1925: Query cache is globally disabled and you can't enable it only for this session
  • SELECT SQL_CACHE ... (original) (MySQL 5.7): warning 1681: 'SQL_CACHE' is deprecated and will be removed in a future release.
  • SELECT SQL_CACHE ... (original) (MySQL 8.0, MySQL 8.4, MySQL 9): error 1054: Unknown column 'SQL_CACHE' in 'field list'
  • SELECT SQL_NO_CACHE ... (original) (MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): warning 1681: 'SQL_NO_CACHE' is deprecated and will be removed in a future release.

The original article

First published 2010-08-31. The text below is the original; the table above shows how it behaves on current versions.

This parameter can either be set at a global level (which implies that all queries take this on as a default parameter) or at a session level.

The query cache in MySQL stores both the SQL and the returned rows of the query. This is particularly useful if the same query and results are returned numerous times. The query cache is shared among the different sessions so the speed up in performance can be for all sessions. Any time a change occurs on a table i.e. an insert, update or delete the data becomes stale and the cache is flushed of the results.

The query_cache_type allows you to define the behaviour of your queries with the query cache.

Setting the value to: 0 - switches off the caching element, in which case the MySQL will always reprocess every execution of the SQL and obtains its results directly from the table.

1 - The caching occurs for all SQL except those that explicitly have indicated that no caching is to take place. SELECT SQL_NO_CACHE c1, c2 FROM t1 WHERE c1 = 1;

2 - The caching is available only on demand i.e. where you explicitly state it in the SELECT statement. SELECT SQL_CACHE c1, c2 FROM t1 WHERE c1 = 1;

To show the power of the caching, I have setup a table consisting of 1,000,000 rows which we will perform a group by operation on. The first operation will not cache the results and we will execute the same operation a second time and see the timing differences:

CREATE TABLE T_TEST (name VARCHAR(255), result VARCHAR(255));

We then generated 1,000,000 rows of random data with name set to either test1, test2, test3 or test4. We then performed the following operations against that data:

select SQL_NO_CACHE name, count(1) as occurrences
from T_TEST
group by name;

Each iteration takes on average 1.14 seconds to complete.

The next iteration uses the following code:

select SQL_CACHE name, count(1) as occurrences
from T_TEST
group by name;

Note that the SQL cache was flushed prior to the run. This was done by adding and removing an entry to the table which makes the cached data stale. The first iteration of this SQL statement took 1.43 seconds to complete but subsequent executions of the exact same SQL statement took 0.008 seconds due to the caching behaviour.

Run # No caching Caching
1 1.437 1.436
2 1.108 0.008
3 1.143 0.008
4 1.106 0.009
5 1.119 0.009
6 1.108 0.008
7 1.107 0.008
8 1.110 0.008
9 1.109 0.008
10 1.115 0.008
Average 1.1462 0.151