With the FLUSH QUERY CACHE command you can defragment the query cache to better utilize its memory. This command will not remove any queries from the cache. FLUSH TABLES also flushes the query cache.
The RESET QUERY CACHE command removes all query results from the query cache.
You can check whether the query cache is present in your MySQL version:
mysql> SHOW VARIABLES LIKE 'have_query_cache'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | have_query_cache | YES | +------------------+-------+ 1 row in set (0.00 sec)
You can monitor query cache performance in SHOW STATUS:
Variable | Description |
Qcache_queries_in_cache | Number of queries registered in the cache. |
Qcache_inserts | Number of queries added to the cache. |
Qcache_hits | Number of cache hits. |
Qcache_lowmem_prunes | Number of queries that were deleted from cache because of low memory. |
Qcache_not_cached | Number of non-cached queries (not cachable, or due to QUERY_CACHE_TYPE). |
Qcache_free_memory | Amount of free memory for query cache. |
Qcache_free_blocks | Number of free memory blocks in query cache. |
Qcache_total_blocks | Total number of blocks in query cache. |
Total number of queries = Qcache_inserts + Qcache_hits + Qcache_not_cached.
The query cache uses variable length blocks, so Qcache_total_blocks and Qcache_free_blocks may indicate query cache memory fragmentation. After FLUSH QUERY CACHE only a single (big) free block remains.
Note: Every query needs a minimum of two blocks (one for the query text and one or more for the query results). Also, every table that is used by a query needs one block, but if two or more queries use same table only one block needs to be allocated.
You can use the Qcache_lowmem_prunes status variable to tune the query cache size. It counts the number of queries that have been removed from the cache to free up memory for caching new queries. The query cache uses a least recently used (LRU) strategy to decide which queries to remove from the cache.