---
title: "如何使用MySQL的Query Cache"
author: "swincle"
date: "2013-07-04T15:18:15Z"
tags: []
categories: ["数据库"]
slug: "mysql-query-cache"
draft: false
lastmod: "2013-07-04T15:18:15Z"
id: 1899589
url: "https://swincle.iteye.com/blog/1899589"
---

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

几个重要的MySQL Query Cache参数：

mysql&gt; 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&gt; 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&gt; show variables like ‘%query\_cache\_type%’;  
+——————+——-+  
| Variable\_name | Value |  
+——————+——-+  
| query\_cache\_type | OFF |  
+——————+——-+  
1 row in set (0.00 sec)

mysql&gt; set @@global.query\_cache\_type = on;  
Query OK, 0 rows affected (0.00 sec)

mysql&gt; 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&gt; show variables like ‘%query\_cache\_size%’;  
+——————+——-+  
| Variable\_name | Value |  
+——————+——-+  
| query\_cache\_size | 0 |  
+——————+——-+  
1 row in set (0.00 sec)

mysql&gt; set @@global.query\_cache\_size = 2\*1024\*1024;  
Query OK, 0 rows affected (0.00 sec)

mysql&gt; 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&gt; 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&gt; select count(1) FROM tb\_DBA\_test;  
+———-+  
| count(1) |  
+———-+  
| 890089 |  
+———-+  
1 row in set (0.00 sec)

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

mysql&gt; 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&gt; select count(1) FROM tb\_DBA\_test;  
+———-+  
| count(1) |  
+———-+  
| 890089 |  
+———-+  
1 row in set (0.00 sec)

 

**5.查看MySQL Query Cache状态**

mysql&gt; 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&gt; flush query cache;  
Query OK, 0 rows affected (0.00 sec)

**2.Reset Query Cache**

清空查询缓存的命令。

mysql&gt; 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&gt; reset query cache;  
Query OK, 0 rows affected (0.00 sec)

mysql&gt; 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)