07月24, 2020
收藏本站

MySQL之查询缓存

故事:起源于一次面试,问select sql_no_cache是不使用缓存,还是查询的结果不写入缓存?当时也是一时懵了,但根据多年使用sql_no_cache的经验来看,是不会使用缓存,也不会写入缓存,并不用每次复现慢SQL都去reset query cache。后来查资料也证明经验是对的。

几个用法:

  1. 开启查询缓存:query_cache_type=1
  2. 清理查询缓存:reset query cache
  3. 查询不使用缓存:select sql_no_cache * from tb

如何向别人证明使用select sql_no_cache真的没经过缓存??
执行show global status like 'Qcache_hits';show global status like 'Com_select';

  • root test>show global status like 'Qcache_hits';
  • +---------------+-------+
  • | Variable_name | Value |
  • +---------------+-------+
  • | Qcache_hits | 1 |
  • +---------------+-------+
  • 1 row in set (0.01 sec)
  • root test>show global status like 'Com_select';
  • +---------------+-------+
  • | Variable_name | Value |
  • +---------------+-------+
  • | Com_select | 4 |
  • +---------------+-------+
  • 1 row in set (0.00 sec)
asciidoc

再执行要查询的SQL,select sql_no_cache * from tb
最后再执行show global status like 'Qcache_hits';show global status like 'Com_select';
如果前后的Qcache_hits值没变,则说明没经过缓存或没命中缓存。


Comments