MySQL Query Cache故名思义就是用来缓存和查询相关的数据的。听说这个东东很好用,于是趁着博客搬迁的时候顺便在VPS开启了。而且开启了Memcache,现在已经是内存级的数据库缓存了。不过还是想加一个webserver cache,最好是nginx,因为对这个稍微熟悉一点,过作为缓存的话varnish会比nginx好很多,要是有空的话还是想学习下VCL。回到正题,接下来就记录下MySQL Query Cache的一些参数,以及怎么去设置等问题…

几个重要的MySQL Query Cache参数:

mysql> show variables like ‘%query_cache%’;
+——————————+———+
| Variable_name | Value |
+——————————+———+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+——————————+———+
6 rows in set (0.00 sec)

 

1.query_cache_limit:允许使用MySQL Query Cache 的单条Query 结果集的最大容量,默认是1MB。当超过此参数设置的Query结果集大小的时候,MySQL将没有办法使用 Query Cache。

2.query_cache_min_res_unit:设置 Query Cache 中每次分配内存的最小空间大小,也就是每个Query 的Cache 最小占用的内存空间大小。

3.query_cache_size:Query Cache所使用的内存大小,默认为0(其实就是没有使用Query Cache)。这个值必须是1024的整数倍,如果不是1024的整数倍,则调整为小一级的1024的整数倍。

4.query_cache_type:是否使用MySQL Query Cache的功能性开关,它有三种可能性的值

 

  • 0:关闭MySQL Query Cache功能,任何情况下不会使用MySQL Query Cache功能。
  • 1:开启MySQL Query Cache功能,仅当SELECT语句中使用SELECT SQL_NO_CANCHE时,不用Query Cache
  • 2:开启MySQL Query Cache功能,仅当SELECT语句中使用SELECT SQL_CANCHE时,用Query Cache

5.query_cache_wlock_invalidate:当有写锁定发生在表上时,是否先失效该表相关的Query Cache。

  • 如果设置为1(TRUE):则在写锁定的同时将失效该表相关的所有 Query Cache
  • 如果设置为0(FALSE):则在锁定时刻仍然允许读取该表相关的 Query Cache

几个重要的MySQL Query Cache状态值

mysql> show status like ‘%Qcache%’;
+————————-+——-+
| Variable_name | Value |
+————————-+——-+
| Qcache_free_blocks | 0 |缓存中有多少未被使用空闲的内存块
| Qcache_free_memory | 0 |可用的缓存空间
| Qcache_hits | 0 |缓存命中的次数
| Qcache_inserts | 0 |没有使用缓存的查询次数,也就是没有命中的次数
| Qcache_lowmem_prunes | 0 |由于内存不足导致被删除的缓存条目数量
| Qcache_not_cached | 0 |无法被缓存的查询的数量,这个值越小越好
| Qcache_queries_in_cache | 0 |当前被cache的查询数量
| Qcache_total_blocks | 0 |当前使用的内存块的数量
+————————-+——-+
8 rows in set (0.00 sec)

 

如何设置MySQL Query Cache参数

1.开启query_cache_type

mysql> show variables like ‘%query_cache_type%’;
+——————+——-+
| Variable_name | Value |
+——————+——-+
| query_cache_type | OFF |
+——————+——-+
1 row in set (0.00 sec)

mysql> set @@global.query_cache_type = on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like ‘%query_cache_type%’;
+——————+——-+
| Variable_name | Value |
+——————+——-+
| query_cache_type | ON |
+——————+——-+
1 row in set (0.00 sec)

 

2.设置query_cache_size值大小

mysql> show variables like ‘%query_cache_size%’;
+——————+——-+
| Variable_name | Value |
+——————+——-+
| query_cache_size | 0 |
+——————+——-+
1 row in set (0.00 sec)

mysql> set @@global.query_cache_size = 2*1024*1024;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like ‘%query_cache_size%’;
+——————+———+
| Variable_name | Value |
+——————+———+
| query_cache_size | 2097152 |
+——————+———+
1 row in set (0.00 sec)

 

 

MySQL Query Cache参数值变化情况

1.没有执行查询语句之前的MySQL Query Cache状态

mysql> show status like ‘%Qcache%’;
+————————-+———+
| Variable_name | Value |
+————————-+———+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2088472 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+————————-+———+
8 rows in set (0.00 sec)

2.执行查询语句

mysql> select count(1) FROM tb_DBA_test;
+———-+
| count(1) |
+———-+
| 890089 |
+———-+
1 row in set (0.00 sec)

3.执行查询语句之后的MySQL Query Cache状态

mysql> show status like ‘%Qcache%’;
+————————-+———+
| Variable_name | Value |
+————————-+———+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2086936 |
| Qcache_hits | 0 |
| Qcache_inserts | 1 |查询缓存生效,已经放入缓存
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+————————-+———+
8 rows in set (0.00 sec)

 

4.再次执行查询语句

mysql> select count(1) FROM tb_DBA_test;
+———-+
| count(1) |
+———-+
| 890089 |
+———-+
1 row in set (0.00 sec)

 

5.查看MySQL Query Cache状态

mysql> show status like ‘%Qcache%’;
+————————-+———+
| Variable_name | Value |
+————————-+———+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2086936 |
| Qcache_hits | 1 |缓存命中率增加1
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+————————-+———+
8 rows in set (0.00 sec)

 

清空MySQL Query Cache

1.Flush Query Cache

这个操作不会删除缓存的内容,它是把所有的存储块向上移动,把所有的空闲块向下移动合并到可用内存中去。它在运行时候会锁定整个服务器,阻止访问缓存,但通常这个操作很快,除非缓存的内容很大。

mysql> flush query cache;
Query OK, 0 rows affected (0.00 sec)

2.Reset Query Cache

清空查询缓存的命令。

mysql> show status like ‘%Qcache%’;
+————————-+———+
| Variable_name | Value |
+————————-+———+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2086936 |
| Qcache_hits | 2 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+————————-+———+
8 rows in set (0.00 sec)

mysql> reset query cache;
Query OK, 0 rows affected (0.00 sec)

mysql> show status like ‘%Qcache%’;
+————————-+———+
| Variable_name | Value |
+————————-+———+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2088472 |
| Qcache_hits | 2 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+————————-+———+
8 rows in set (0.00 sec)