mysql参考文档中关于query cache的配置的说明:
http://dev.mysql.com/doc/refman/5.7/en/query-cache-configuration.html
与查询缓存相关的参数一共有3个,第一个就是have_query_cache,如果只是看到这个值设置为YES就觉得查询缓存开启了就大错特错了。默认情况下这个值一直都是YES。
另外两个相关的值是query_cache_size和query_cache_type,如果query_cache_size设置为0,那么查询缓存也是关闭的。query_cache_size应该设置足够大的值,一定要大于40k,因为40k是它自身程序需要的容量。
query_cache_type有三个值:
-
A value of
0
orOFF
prevents caching or retrieval of cached results. 关闭状态 -
A value of
1
orON
enables caching except of those statements that begin withSELECT SQL_NO_CACHE
. -
cache开启,只有用SELECT SQL_NO_CACHE时才不从cache中读
-
A value of
2
orDEMAND
causes caching of only those statements that begin withSELECT SQL_CACHE
. -
cache开启,但是需要显式存,也就是只存SELECT SQL_CACHE的查询语句。
查询缓存的使用情况可以用以下语句来查询:
SHOW STATUS LIKE 'Qcache%';
以下是结果
# Variable_name, Value
'Qcache_free_blocks', '1'
'Qcache_free_memory', '16747672'
'Qcache_hits', '7'
'Qcache_inserts', '5'
'Qcache_lowmem_prunes', '0'
'Qcache_not_cached', '147'
'Qcache_queries_in_cache', '5'
'Qcache_total_blocks', '15'
mysql的查询缓存开启后,应用层在写SQL的时候应该尽量考虑到能够使用Mysql查询缓存。