码迷,mamicode.com
首页 > 数据库 > 详细

mysql开启缓存

时间:2016-02-28 00:49:57      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

mysql 开启查询缓存可以有两种方法来开启一种是使用set命令来进行开启,另一种是直接修改my.ini文件来直接设置都是非常的简单的哦。

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

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%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。

mysql开启缓存

标签:

原文地址:http://www.cnblogs.com/wangzhuxing/p/5223881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!