3

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.

5

MySQL Documentation has SQL_NO_CACHE option:

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

SQL_CACHE

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

SQL_NO_CACHE

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.)

Examples:

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';

or

SET query_cache_type = 2;

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

1

You cannot. MySQL don't have that mechanism. The common featues which are misunderstood about query cache

  1. Only works for SELECT queries
  2. Queries should be absolutely identical
  3. It is not possible to see query cache contents

We can only optimize the parameters

query_cache_limit = 150K
query_cache_size = 512M
query_cache_type = 1

Rather, we should disable query cache where there is frequent UPDATE and INSERT as once there is any change in table, nullifies all SELECTs under query cache. It is seen that disabling query cache improved performance in these scenarios

1
  • query_cache_size = 512M is dangerously high. This is because of inefficiencies in purging the query cache whenever a write occurs. I recommend no bigger than 50M. – Rick James Mar 20 '15 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.