mysql查询不使用缓存_[MySQL优化案例]系列 -- 无法使用查询缓存

作/译者:叶金荣(Email:

c3f7cf7250583b5dab147ec66529b7bd.gif),来源:http://imysql.cn,转载请注明作/译者和出处,并且不能用于商业用途,违者必究。

1. 先看当前的 QCACHE, hits 和 in_cache 均为 0mysql>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 仍为 1mysql>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 增加了 2mysql>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”部分;另外,存储过程/存储函数/触发器也无法用到查询缓存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值