mysql查询缓存

 
Mysql 查询缓存 
查询缓存的作用就是当查询接收到一个和之前同样的查询,服务器将会从查询缓存种检索结果,而不是再次分析和执行上次的查询。这样就大大提高了性能,节省时间。 
1.配置查询缓存 
修改配置文件,修改[mysqld]下的query_cache_size和query_cache_type(如果没有则添加)。其中query_cache_size表示缓存的大小,而query_cache_type有3个值,表示缓存那种类  型的select结果集,query_cache_type各个值如下: 
0或off关闭缓存 
1或on开启缓存,但是不保存使用sql_no_cache的select语句,如不缓存select  sql_no_cache name from wei where id=2 
2或demand开启有条件缓存,只缓存带sql_cache的select语句,缓存select  sql_cache name from wei where id=4 
例子的配置为下,配置完成重启Mysql服务器即可。 
Java代码   收藏代码
  1. query_cache_size=10M  
  2. query_cache_type=1  

可以用如下命令查看是否开启,其中have_query_cache为是否开启,query_cache_limit 指定单个查询能够使用的缓冲区大小,缺省为1M;query_cache_min_res_unit为系统分配的最小缓存块大小,默认是4KB,设置值大对大数据查询有好处,但如果你的查询都是小数据 查询,就容易造成内存碎片和浪费;query_cache_size和query_cache_type就是上面我们的配置;query_cache_wlock_invalidate表示当有其他客户端正在对MyISAM表进行写操作时,如果查询在query cache中,是否返回cache结果还是等写操作完成再读表获取结果。 
Java代码   收藏代码
  1. mysql> show variables like '%query_cache%';  
  2. +------------------------------+----------+  
  3. | Variable_name                | Value    |  
  4. +------------------------------+----------+  
  5. | have_query_cache             | YES      |  
  6. | query_cache_limit            | 1048576  |  
  7. | query_cache_min_res_unit     | 4096     |  
  8. | query_cache_size             | 10485760 |  
  9. | query_cache_type             | ON       |  
  10. | query_cache_wlock_invalidate | OFF      |  
  11. +------------------------------+----------+  
  12. 6 rows in set (0.00 sec)  


2.测试 
我们先执行一次,select  count(*) from wei ;然后再执行一次,可以看出第二次用的时间远远低于第一次的执行,因为第二次从缓存中读取了select结果。 
Java代码   收藏代码
  1. mysql> select  count(*) from wei ;  
  2. +----------+  
  3. | count(*) |  
  4. +----------+  
  5. |  4194304 |  
  6. +----------+  
  7. 1 row in set (3.92 sec)  
  8.   
  9. mysql> select  count(*) from wei ;  
  10. +----------+  
  11. | count(*) |  
  12. +----------+  
  13. |  4194304 |  
  14. +----------+  
  15. 1 row in set (0.00 sec)  


我们可以通过如下命令查看现在缓存的情况 

Java代码   收藏代码
  1. mysql> show status like 'qcache%';  
  2. +-------------------------+----------+  
  3. | Variable_name           | Value    |  
  4. +-------------------------+----------+  
  5. | Qcache_free_blocks      | 1        |  
  6. | Qcache_free_memory      | 10475424 |  
  7. | Qcache_hits             | 1        |  
  8. | Qcache_inserts          | 1        |  
  9. | Qcache_lowmem_prunes    | 0        |  
  10. | Qcache_not_cached       | 0        |  
  11. | Qcache_queries_in_cache | 1        |  
  12. | Qcache_total_blocks     | 4        |  
  13. +-------------------------+----------+  
  14. 8 rows in set (0.00 sec)  

其中各个参数的意义如下: 
Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。 
Qcache_free_memory:缓存中的空闲内存。 
Qcache_hits:每次查询在缓存中命中时就增大 
Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。 
Qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个 数字在不断增长,就表示可能碎片非常严重,或者内存很少。(上面的 free_blocks和free_memory可以告诉您属于哪种情况) 
Qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 SELECT 语句或者用了now()之类的函数。 
Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。 
Qcache_total_blocks:缓存中块的数量。
 
mysql 开启查询缓存可以有两种方法来开启一种是使用set命令来进行开启,另一种是直接修改my.ini文件来直接设置都是非常的简单的哦。
<iframe id="aswift_0" style="left: 0px; position: absolute; top: 0px;" name="aswift_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="300" height="250"></iframe>
<iframe id="cproIframe_u1339684_1" src="http://pos.baidu.com/acom?adn=3&amp;at=134&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=GBK&amp;cfv=18&amp;ch=0&amp;col=zh-CN&amp;conOP=0&amp;cpa=1&amp;dai=1&amp;dis=0&amp;layout_filter=rank%2Ctabcloud&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DVO3uBrVvqviOfus93PbDYXXOhG3_gztRKcGWXorhtvYQKUe3mZN-J-kNNPrN5b5yEyrBh4OeqxZhB6-Mcg0CWa%26wd%3D%26eqid%3Dcaa45ed400007b250000000355c1ba44&amp;ltu=http%3A%2F%2Fwww.111cn.net%2Fdatabase%2Fmysql%2F63815.htm&amp;lunum=6&amp;n=mailangel123_cpr&amp;pcs=1349x677&amp;pis=10000x10000&amp;ps=434x525&amp;psr=1366x768&amp;pss=1349x725&amp;qn=573287b0d6c25b2a&amp;rad=&amp;rsi0=300&amp;rsi1=250&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23FFFFFF&amp;rss2=%230000FF&amp;rss3=%23444444&amp;rss4=%23008000&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;td_id=1339684&amp;tn=text_default_300_250&amp;tpr=1438759491024&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u1339684&amp;ti=mysql%20%E5%BC%80%E5%90%AF%E6%9F%A5%E8%AF%A2%E7%BC%93%E5%AD%98%E6%96%B9%E6%B3%95%E4%B8%8E%E6%9F%A5%E8%AF%A2%E4%BE%8B%E5%AD%90-mysql%E6%95%99%E7%A8%8B-%E6%95%B0%E6%8D%AE%E5%BA%93-%E5%A3%B9%E8%81%9A%E6%95%99%E7%A8%8B%E7%BD%91&amp;tt=1438759491005.21.149.154" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center,center" width="300" height="250"></iframe>

开启缓存,设置缓存大小,具体实施如下:

1、修改配置文件,windows下是my.ini,linux下是my.cnf;

在配置文件的最后追加上:

 代码如下 复制代码

query_cache_type = 1
query_cache_size = 600000

需要重启mysql生效;

那么采用第二种方式;

b) 开启缓存,两种方式:

a)使用mysql命令:

 代码如下 复制代码

set global query_cache_type = 1;  
set global query_cache_size = 600000;

如果报错:

query cache is disabled; restart the server with query_cache_type=1...

在mysql命令行输入

show variables like "%query_cache%" 查看是否设置成功,现在可以使用缓存了;
当然如果你的数据表有更新怎么办,没关系mysql默认会和这个表有关系的缓存删掉,下次查询的时候会直接读表然后再缓存

下面是一个简单的例子:

 代码如下 复制代码

[MySQL@csdba1850 ~]$ MySQL -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 3
Server version: 5.0.45-community MySQL Community Edition (GPL)
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
MySQL> set global query_cache_size = 600000;

设置缓存内存

Query OK, 0 rows affected (0.00 sec)
MySQL> set session query_cache_type = ON;

开启查询缓存

Query OK, 0 rows affected (0.00 sec)
MySQL> use test Reading table information for completion 
of table and column names You can turn off this feature to 
get a quicker startup with -A Database changed mysql> show tables; 
+----------------+ | Tables_in_test | +----------------+ | animals | 
| person | +----------------+ 5 rows in set (0.00 sec) 
mysql> select count(*) from animals; +----------+ | count(*) 
| +----------+ | 6 | +----------+ 1 row in set (0.00 sec)

Qcache_hits表示mysql缓存查询在缓存中命中的累计次数,是累加值。


mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ 
| Variable_name | Value | +---------------+-------+ | Qcache_hits 
| 0 | --0次 +---------------+-------+ 8 rows in set (0.00 sec) 
mysql> select count(*) from animals; +----------+ | count(*) 
| +----------+ | 6 | +----------+ 1 row in set (0.00 sec) 
mysql> SHOW STATUS LIKE 'Qcache%'; +---------------+-------+ 
| Variable_name | Value | +---------------+-------+ | Qcache_hits | 1 | 

表示sql在缓存中直接得到结果,不需要再去解析

+---------------+-------+ 8 rows in set (0.00 sec) 
mysql> select count(*) from animals; +----------+ 
| count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) 
mysql> select count(*) from animals; +----------+ | count(*) 
| +----------+ | 6 | +----------+ 1 row in set (0.00 sec) 
mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ 
| Variable_name | Value | +---------------+-------+ | Qcache_hits | 3 | 

上面的sql也是是从缓存中直接取到结果

+---------------+-------+ 1 row in set (0.00 sec) mysql> insert into animals select 9,'testsds' ; 

插入数据后,跟这个表所有相关的sql缓存就会被清空掉

Query OK, 1 row affected (0.00 sec) Records: 
1 Duplicates: 0 Warnings: 0 mysql> select count(*) from animals; 
+----------+ | count(*) | +----------+ | 7 | +----------+ 
1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; 
+---------------+-------+ | Variable_name | Value | 
+---------------+-------+ | Qcache_hits | 3 | 

还是等于3,说明上一条sql是没有直接从缓存中直接得到的

+---------------+-------+ 1 row in set (0.00 sec) 
mysql> select count(*) from animals; +----------+ 
| count(*) | +----------+ | 7 | +----------+ 
1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; 
+---------------+-------+ | Variable_name | Value | +---------------+-------+ 
| Qcache_hits | 4 | +---------------+-------+ 1 row in set (0.00 sec) 

以上的相关内容就是对mysql缓存查询和设置的介绍,望你能有所收获。

补充

第一: query_cache_type 使用查询缓存的方式
一般,我们会把 query_cache_type 设置为 ON,默认情况下应该是ON

 代码如下 复制代码
mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| ON |
+--------------------+

query_cache_type有3个值 0代表关闭查询缓存OFF,1代表开启ON,2(DEMAND)代表当sql语句中有SQL_CACHE关键词时才缓存,如:select SQL_CACHE user_name from users where user_id = '100';
这样 当我们执行 select id,name from tableName; 这样就会用到查询缓存。
①在 query_cache_type 打开的情况下,如果你不想使用缓存,需要指明
select sql_no_cache id,name from tableName;
②当sql中用到mysql函数,也不会缓存
 
当然也可以禁用查询缓存: mysql> set session query_cache_type=off;
第二: 系统变量 have_query_cache 设置查询缓存是否可用

 代码如下 复制代码
mysql> show variables like 'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |

+------------------+-------+
上面的显示,表示设置查询缓存是可用的。
第三: 系统变量 query_cache_size
表示查询缓存大小,也就是分配内存大小给查询缓存,如果你分配大小为0,
那么 第一步 和 第二步 起不到作用,还是没有任何效果。

 代码如下 复制代码
mysql> select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 16777216 |
+---------------------------+

上面是 mysql6.0设置默认的,之前的版本好像默认是0的,那么就要自己设置下。
设置 set @@global.query_cache_size=1000000; 这里是设置1M左右,900多K。
再次查看下

 代码如下 复制代码
select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 999424 |
+---------------------------+

显示我们设置新的大小,表示设置成功。
第四: query_cache_limit 控制缓存查询结果的最大值
例如: 如果查询结果很大, 也缓存????这个明显是不可能的。
MySql 可以设置一个最大的缓存值,当你查询缓存数结果数据超过这个值就不会
进行缓存。缺省为1M,也就是超过了1M查询结果就不会缓存。

 代码如下 复制代码
mysql> select @@global.query_cache_limit;
+----------------------------+
| @@global.query_cache_limit |
+----------------------------+
| 1048576 |
+----------------------------+

这个是默认的数值,如果需要修改,就像设置缓存大小一样设置,使用set
重新指定大小。
好了,通过4个步骤就可以 打开了查询缓存,具体值的大小和查询的方式 这个因不同
的情况来指定了。
mysql查询缓存相关变量

 代码如下 复制代码
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             | 16777216 |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
6 rows in set (0.00 sec)

第五:查看缓存的状态

 代码如下 复制代码
mysql> 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)

MySQL 提供了一系列的 Global Status 来记录 Query Cache 的当前状态,具体如下:
Qcache_free_blocks:目前还处于空闲状态的 Query Cache 中内存 Block 数目
Qcache_free_memory:目前还处于空闲状态的 Query Cache 内存总量
Qcache_hits:Query Cache 命中次数
Qcache_inserts:向 Query Cache 中插入新的 Query Cache 的次数,也就是没有命中的次数
Qcache_lowmem_prunes:当 Query Cache 内存容量不够,需要从中删除老的 Query Cache 以给新的 Cache 对象使用的次数
Qcache_not_cached:没有被 Cache 的 SQL 数,包括无法被 Cache 的 SQL 以及由于 query_cache_type 设置的不会被 Cache 的 SQL
Qcache_queries_in_cache:目前在 Query Cache 中的 SQL 数量
Qcache_total_blocks:Query Cache 中总的 Block 数量
 
第六:检查查询缓存使用情况
检查是否从查询缓存中受益的最简单的办法就是检查缓存命中率
当服务器收到SELECT 语句的时候,Qcache_hits 和Com_select 这两个变量会根据查询缓存
的情况进行递增
查询缓存命中率的计算公式是:Qcache_hits/(Qcache_hits + Com_select)。

 代码如下 复制代码
mysql> 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 > 50的话说明query_cache_size可能有点小,要不就是碎片太多。

查询缓存命中率 = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%

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

猜你喜欢

转载自peter8015.iteye.com/blog/2233018