1. 先看当前的 QCACHE, hits 和 in_cache 均为 0
mysql>SHOW GLOBAL STATUS LIKE 'QCACHE%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_hits | 0 | | Qcache_not_cached | 1 | | Qcache_queries_in_cache | 0 | +-------------------------+----------+ mysql>SELECT * FROM `Weather` WHERE `AddTime` = 1185897600;
2. 第一次查询完之后,再来看下,not_cached 增加了 1, in_cache 变成 1,因为 SHOW GLOBAL STATUS LIKE 'QCACHE%' 本身不会放在 QCACHE 里的
mysql>SHOW GLOBAL STATUS LIKE 'QCACHE%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_hits | 0 | | Qcache_not_cached | 2 | | Qcache_queries_in_cache | 1 | +-------------------------+----------+ mysql>SELECT * FROM `Weather` WHERE `AddTime` = 1185897600;
3. 第二次查询完后,再来看看,hits 加 1,in_cache 仍为 1
mysql>SHOW GLOBAL STATUS LIKE 'QCACHE%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_hits | 1 | | Qcache_not_cached | 3 | | Qcache_queries_in_cache | 1 | +-------------------------+----------+ mysql>SELECT * FROM `Weather` WHERE `AddTime` = DATE_FORMAT(NOW(), '%Y-%m-%d');
4. 执行完上面的查询后看一下,QCACHE 没工作,in_cache 仍为 1,not_cached 增加了 2
mysql>SHOW GLOBAL STATUS LIKE 'QCACHE%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_hits | 1 | | Qcache_not_cached | 5 | | Qcache_queries_in_cache | 1 | +-------------------------+----------+ mysql>SELECT * FROM `Weather` WHERE `AddTime` = DATE_FORMAT(NOW(), '%Y-%m-%d');
5. 再验证一下,跟刚才的结论一样
mysql>SHOW GLOBAL STATUS LIKE 'QCACHE%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_hits | 1 | | Qcache_not_cached | 7 | | Qcache_queries_in_cache | 1 | +-------------------------+----------+
总结,如果查询语句条件中带有 CURDATE()、CURRENT_TIME()、FOUND_ROWS() 等函数时,则无法试用查询缓存。具体的请查看MySQL手册的“5.13.1.?How the Query Cache Operates”部分;另外,存储过程/存储函数/触发器也无法用到查询缓存。
本文转自叶金荣51CTO博客,原文链接:http://blog.51cto.com/imysql/308357,如需转载请自行联系原作者