如何开启MySQL的中的Query Cache缓存

2016年5月13日

MySQL Query Cache是mysql中的一个功能,主要是用来缓存和查询相关数据,本文文详细介绍如何开启MySQL中的Query Cache,以及Query Cache中的一些参数。

MySQL Query Cache的参数:

1
2
3
4
5
6
7
8
9
10
11
12
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 rowsinset(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的功能性开关,它有三种可能性的值

1:关闭MySQL Query Cache功能,任何情况下不会使用MySQL Query Cache功能。
2:开启MySQL Query Cache功能,仅当SELECT语句中使用SELECT SQL_NO_CANCHE时,不用Query Cache
3:开启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)

3.查看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)

这样就完成了开启Query Cache缓存的动作,下面就是一些常用的命令,可以不看!

4.清空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)

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注