406

I'm testing the speed of some queries in MySQL. The database is caching these queries making it difficult for me to get reliable results when testing how fast these queries are.

Is there a way to disable caching for a query?

System: MySQL 4 on Linux webhosting, I have access to PHPMyAdmin.

Thanks

11 Answers 11

607

Try using the SQL_NO_CACHE (MySQL 5.7) option in your query. (MySQL 5.6 users click HERE )

eg.

SELECT SQL_NO_CACHE * FROM TABLE

This will stop MySQL caching the results, however be aware that other OS and disk caches may also impact performance. These are harder to get around.

Sign up to request clarification or add additional context in comments.

10 Comments

A nice article about mysql Query cache. How to setup and see the cache in action! Worth the read. databasejournal.com/features/mysql/article.php/3110171/…
Before trying to improve performance, try restarting your mysql server. It may be that some process is affecting everything. It happened with me. Although it is a comment that has no direct connection with the issue, it can help many people.
@Brett see SalmanPK's comment way back in '11. That doc page is literally 4 sentences and answers your question directly. In case the page is removed in future: "It neither checks the query cache to see whether the result is already cached, nor does it cache the query result."
The query cache is deprecated as of MySQL 5.7.20, and is removed in MySQL 8.0.
|
141

Another alternative that only affects the current connection:

SET SESSION query_cache_type=0;

3 Comments

My bad. It was query_cache_size I was looking at, not query_cache_type. I was mixing up yours and SeniorDev's answer.
Neither of those options seem to work in MySQL 8 ... ?
In MySQL 8, query cache does not exist anymore
74

Any reference to current date/time will disable the query cache for that selection:

SELECT *,NOW() FROM TABLE

See "Prerequisites and Notes for MySQL Query Cache Use" @ http://dev.mysql.com/tech-resources/articles/mysql-query-cache.html

Comments

32

There is also configuration option: query_cache_size=0

To disable the query cache at server startup, set the query_cache_size system variable to 0. By disabling the query cache code, there is no noticeable overhead. If you build MySQL from source, query cache capabilities can be excluded from the server entirely by invoking configure with the --without-query-cache option.

See http://dev.mysql.com/doc/refman/5.1/en/query-cache.html

Comments

25

You can also run the follow command to reset the query cache.

RESET QUERY CACHE

2 Comments

This is probably overkill... ideally, you only want MySQL to temporarily ignore the cache.
You also need special permissions for this.
20

One problem with the

SELECT SQL_NO_CACHE * FROM TABLE

method is that it seems to only prevent the result of your query from being cached. However, if you're querying a database that is actively being used with the query you want to test, then other clients may cache your query, affecting your results. I am continuing to research ways around this, will edit this post if I figure one out.

2 Comments

But if your cache is filled up by others, you will get the illusion of being fast. But sometimes not.
Actually, the MySQL documentation says: "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."
15

I'd Use the following:

SHOW VARIABLES LIKE 'query_cache_type';
SET SESSION query_cache_type = OFF;
SHOW VARIABLES LIKE 'query_cache_type';

Comments

7

Using a user-defined variable within a query makes the query resuts uncacheable. I found it a much better indicator than using SQL_NO_CACHE. But you should put the variable in a place where the variable setting would not seriously affect the performance:

SELECT t.*
FROM thetable t, (SELECT @a:=NULL) as init;

3 Comments

"I found it a much better indicator than using SQL_NO_CACHE." How so? Seems you would need a pretty strong case for using an obscure hack over an explicit keyword, unless the explicit keyword isn't doing what it claims.
@Air I found this solution to work better than SQL_NO_CACHE as well. SQL_NO_CACHE worked for the first time running the query, but then after that it did not work again. I'm assuming this is due to other mechanisms of caching. I think this works better because a search that relies on a variable can't be cached by any layer or mechanism--at least, that's my best guess.
Upvoted. For me, SQL_NO_CACHE or /*!40001 SQL_NO_CACHE */ made no difference at all compared to regular query. However I could see an increase of query duration time of 50% by using , (SELECT @a:=NULL). Anyways, This does not avoid the OS/Disk cache. See Ian's answer. Still, a worthy improvement. Thanks!
4

Whilst some of the answers are good, there is a major caveat.

The mysql queries may be prevented from being cached, but it won't prevent your underlying O.S caching disk accesses into memory. This can be a major slowdown for some queries especially if they need to pull data from spinning disks.

So whilst it's good to use the methods above, I would also try and test with a different set of data/range each time, that's likely not been pulled from disk into disk/memory cache.

Comments

2

If you want to disable the Query cache set the 'query_cache_size' to 0 in your mysql configuration file . If its set 0 mysql wont use the query cache.

Comments

0

You must change SQL string. Because SQL string is a cache key. For example, add a timestamp to a SQL comment.

Function for PHP:

function db_RunSQL($SQL, $NoCacheMode=false)
{
$SQL = (($NoCacheMode) ? '/*'.time().'*/ ' : '') . $SQL;
return mysqli_query(db_SavedConnect(), $SQL);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.