Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL flush query cache

I have a slow database query that runs 18 seconds for the first run and 4 seconds as any subsequent run. I am trying to optimize it's "coldstart" execution time. But not able to reproduce it continuously.

select SQL_NO_CACHE directive doesn't help.

None of commands below make it run 18 seconds again:

FLUSH QUERY CACHE;
RESET QUERY CACHE;
FLUSH TABLES;

Even database restart doesn't make it run long again.

Is there any other command that flushes cache?

Table engine is InnoDB.

like image 628
Bogdan Gusiev Avatar asked Mar 07 '13 09:03

Bogdan Gusiev


People also ask

How do I clear the query cache in SQL server?

To clear SQL Server's cache, run DBCC DROPCLEANBUFFERS , which clears all data from the cache. Then run DBCC FREEPROCCACHE , which clears the stored procedure cache.

What does flush table do in MySQL?

FLUSH TABLES also removes all query results from the query cache, like the RESET QUERY CACHE statement. For information about query caching and prepared statement caching, see Section 8.10. 3, “The MySQL Query Cache”.

Does MySQL cache query results?

The MySQL query cache is a query results cache. It compares incoming queries that start with SEL to a hash table, and if there is a match returns the results from the previous execution of the query. There are some restrictions: The query must match byte-for-byte (the query cache avoids parsing)

How do I query cache in MySQL?

MySQL determines the queries to cache by examining the query_cache_type variable. Setting this value to 0 or OFF prevents caching or retrieval of cached queries. You can also set it to 1 to enable caching for all queries except for ones beginning with the SELECT SQL_NO_CACHE statement.


1 Answers

Let me explain why its not helping.

Your Requirement : Here you are trying to run the same query again, but you want it to perform as its running first time only by cleaning the cache.

When a query runs, there are multiple type of cache which comes into the picture. 'Query Cache' the most common cache which we talk about, then there are MySQL Caches (e.g. Innodb Buffer Pool ), table_cache (at MySQL level & at InnoDB level as well), OS Caches, Hardware Cache.

select SQL_NO_CACHE : This will prevent running query to save any 'Query cache', this means if you will run same query again that wont have any 'Query Cache', but other caches will be there in picture.

FLUSH QUERY CACHE : this just Defragment the 'query cache' to better utilize its memory

RESET QUERY CACHE : Removes all query results from the 'Query cache', this wont affect if you used 'SQL_NO_CACHE', in all previous queries.

  • In addition to what you are trying, Few more things you can try to Avoid these caches.
  • MySQL Restart is must for 'MySQL Caches' (Innodb Buffer Pool), there is no other way.
  • set global key_buffer_size=0; to make key buffer size zero
  • set global query_cache_type=0;
  • set global query_cache_size=0;
  • OS level Cache : this post may help you
like image 140
Noushad Avatar answered Sep 29 '22 08:09

Noushad