Mysql – tell MySQL what to cache


Pretty straightforward question:

Let's say I have a specific query that I know is regularly used, can I tell MySQL to always cache it "automatically" … i.e. once the cached query results are flushed the mysqld immediately runs that query again to have the cached data ready for the next user?

Or the other way round: can I tell MySQL to not cache specific queries?
What about other caching options, not necessarily related to queries?

I searched through the web on this but can't really find anything related.

Best Answer

MySQL Documentation has SQL_NO_CACHE option:

Two query cache-related options may be specified in SELECT statements:


The query result is cached if it is cacheable and the value of the query_cache_type system variable is ON or DEMAND.


The server does not use the query cache. It neither checks the query cache to see whether the result is already cached, nor does it cache the query result. (Due to a limitation in the parser, a space character must precede and follow the SQL_NO_CACHE keyword; a nonspace such as a newline causes the server to check the query cache to see whether the result is already cached.)


SELECT SQL_CACHE id, name FROM customer;
SELECT SQL_NO_CACHE id, name FROM customer;

You can set SQL_NO_CACHE as the default for your session by setting query_cache_type

SET query_cache_type = 'DEMAND';


SET query_cache_type = 2;

Afterwards, you must say SELECT SQL_CACHE to make it cache the SELECT