HomeMySQL Page 5 - Advanced MySQL Database Administration
4.10.4 Query Cache Status and Maintenance - MySQL
If you need to administer MySQL, this article gets you off to a good start. In this section, we discuss running multiple MySQL servers on the same machine, and the MySQL Query cache. The final installment of a multi-part series, it is excerpted from chapter four of the book MySQL Administrator's Guide, written by Paul Dubois (Sams; ISBN: 0672326345).
You can check whether the query cache is present in your MySQL server using the following statement:
mysql> SHOW VARIABLES LIKE 'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+
You can defragment the query cache to better utilize its memory with the FLUSH QUERY CACHE statement. The statement does not remove any queries from the cache.
The RESET QUERY CACHE statement removes all query results from the query cache. The FLUSH TABLES statement also does this.
To monitor query cache performance, use SHOW STATUS to view the cache status variables:
Descriptions of each of these variables are given in Section 4.2.4, "Server Status Variables." Some uses for them are described here.
The total number of SELECT queries is equal to:
Com_select
+ Qcache_hits
+ queries with errors found by parser
The Com_select value is equal to:
Qcache_inserts
+ Qcache_not_cached
+ queries with errors found during columns/rights
check
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 free block remains.
Every cached query requires 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 requires one block. However, if two or more queries use the same table, only one block needs to be allocated.
The information provided by the Qcache_lowmem_prunes status variable can help you 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. Tuning information is given in Section 4.10.3, "Query Cache Configuration."