---
title: "mysql查询缓存打开、设置、参数查询、性能变量意思"
author: "tw5566"
date: "2014-09-02T20:05:34Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2014-09-02T20:05:34Z"
id: 2112186
url: "https://tw5566.iteye.com/blog/2112186"
---

一般，我们会把 query\_cache\_type 设置为 ON,默认情况下应该是ON  
mysql&gt; select @@query\_cache\_type;  
+--------------------+  
| @@query\_cache\_type |  
+--------------------+  
| ON |  
+--------------------+  
user\_name from users where user\_id = '100';  
这样 当我们执行 select id,name from tableName; 这样就会用到查询缓存。  
①在 query\_cache\_type 打开的情况下，如果你不想使用缓存，需要指明  
select sql\_no\_cache id,name from tableName;

**第二： 系统变量 have\_query\_cache 设置查询缓存是否可用**

+------------------+-------+  
| Variable\_name | Value |  
+------------------+-------+  
| have\_query\_cache | YES |  
+------------------+-------+  
上面的显示，表示设置查询缓存是可用的。

表示查询缓存大小，也就是分配内存大小给查询缓存，如果你分配大小为0，

mysql&gt; select @@global.query\_cache\_size;  
+---------------------------+  
| @@global.query\_cache\_size |  
+---------------------------+  
| 16777216 |  
+---------------------------+  
上面是 mysql6.0设置默认的，之前的版本好像默认是0的，那么就要自己设置下。

再次查看下 select @@global.query\_cache\_size;  
+---------------------------+  
| @@global.query\_cache\_size |  
+---------------------------+  
| 999424 |  
+---------------------------+  
显示我们设置新的大小，表示设置成功。

例如： 如果查询结果很大， 也缓存？？？？这个明显是不可能的。  
MySql 可以设置一个最大的缓存值，当你查询缓存数结果数据超过这个值就不会  
进行缓存。缺省为1M，也就是超过了1M查询结果就不会缓存。

+----------------------------+  
| @@global.query\_cache\_limit |  
+----------------------------+  
| 1048576 |  
+----------------------------+  
这个是默认的数值，如果需要修改，就像设置缓存大小一样设置，使用set  
重新指定大小。  
好了，通过4个步骤就可以 打开了查询缓存，具体值的大小和查询的方式 这个因不同  
的情况来指定了。

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             | 16777216 |  
| query\_cache\_type             | ON       |  
| query\_cache\_wlock\_invalidate | OFF      |  
+------------------------------+----------+  
6 rows in set (0.00 sec)

mysql&gt; show status like '%Qcache%';  
+-------------------------+----------+  
| Variable\_name           | Value    |  
+-------------------------+----------+  
| Qcache\_free\_blocks      | 11       |  
| Qcache\_free\_memory      | 16610552 |  
| Qcache\_hits             | 10       |  
| Qcache\_inserts          | 155      |  
| Qcache\_lowmem\_prunes    | 0        |  
| Qcache\_not\_cached       | 21       |  
| Qcache\_queries\_in\_cache | 111      |  
| Qcache\_total\_blocks     | 256      |  
+-------------------------+----------+  
8 rows in set (0.00 sec)

Qcache\_free\_blocks：目前还处于空闲状态的 Query Cache 中内存 Block 数目

Qcache\_hits：Query Cache 命中次数

Qcache\_lowmem\_prunes：当 Query Cache 内存容量不够，需要从中删除老的 Query Cache 以给新的 Cache 对象使用的次数

Qcache\_queries\_in\_cache：目前在 Query Cache 中的 SQL 数量

检查是否从查询缓存中受益的最简单的办法就是检查缓存命中率

的情况进行递增

*mysql&gt; show status like '%Com\_select%';*

*| Variable\_name | Value |*

*| Com\_select    | 1     |*

query\_cache\_min\_res\_unit的配置是一柄”双刃剑”，默认是4KB，设置值大对大数据查询有好处，但如果你的查询都是小数据 查询，就容易造成内存碎片和浪费。

查询缓存碎片率 = Qcache\_free\_blocks / Qcache\_total\_blocks * 100%

如果查询缓存碎片率超过20%，可以用FLUSH QUERY CACHE整理缓存碎片，或者试试减小query\_cache\_min\_res\_unit，如果你的查询都是小数据量的话。

查询缓存利用率 = (query\_cache\_size - Qcache\_free\_memory) / query\_cache\_size * 100%

查询缓存利用率在25%以下的话说明query\_cache\_size设置的过大，可适当减小;查询缓存利用率在80%以上而且 Qcache\_lowmem\_prunes &gt; 50的话说明query\_cache\_size可能有点小，要不就是碎片太多。

查询缓存命中率 = (Qcache\_hits - Qcache\_inserts) / Qcache\_hits * 100%

示例服务器 查询缓存碎片率 = 20.46%，查询缓存利用率 = 62.26%，查询缓存命中率 = 1.94%，命中率很差，可能写操作比较频繁吧，而且可能有些碎片。

引用一段前辈的话

优化提示：  
如果Qcache\_lowmem\_prunes 值比较大，表示查询缓存区大小设置太小，需要增大。  
如果Qcache\_free\_blocks 较多，表示内存碎片较多，需要清理，flush query cache  
根据我看的 《High Performance MySQL》中所述，关于query\_cache\_min\_res\_unit大小的调优  
，书中给出了一个计算公式，可以供调优设置参考：  
query\_cache\_min\_res\_unit = (query\_cache\_size - Qcache\_free\_memory) / Qcache\_queries\_in\_cache