mysql系列3、mysql开启缓存、设置缓存大小、缓存过时机制

1、开启缓存

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

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

一、修改配置文件my.ini

windows下是my.ini,linux下是my.cnf;linux

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

query_cache_type = 1
query_cache_size = 600000

须要重启mysql生效;windows

二、命令方式

那么采用第二种方式;缓存

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

a)使用mysql命令:session

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命令行输入ui

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缓存查询和设置的介绍,望你能有所收获。

2、查看是否生效

一、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%,命中率不好,可能写操做比较频繁吧,并且可能有些碎片。

3、缓存使用策略和过时机制

一、缓存条件 

    查询缓存能够看作是SQL文本和查询结果的映射。若是第二次查询的SQL和第一次查询的SQL彻底相同(注意必须是彻底相同,即便多一个空格或者大小写不一样都认为不一样)且开启了查询缓存,那么第二次查询就直接从查询缓存中取结果,能够经过下面的SQL来查看缓存命中次数(是个累加值):
 SHOW STATUS LIKE 'Qcache_hits';

    另外即便彻底相同的SQL,若是使用不一样的字符集、不一样的协议等也会被认为是不一样的查询而分别进行缓存。

 二、缓存数据失效时机

    在表的结构或数据发生改变时,查询缓存中的数据再也不有效。有这些INSERT、UPDATE、 DELETE、TRUNCATE、ALTER TABLE、DROP TABLE或DROP DATABASE会致使缓存数据失效。因此查询缓存适合有大量相同查询的应用,不适合有大量数据更新的应用。

 三、缓存清理

可使用下面三个SQL来清理查询缓存:
一、FLUSH QUERY CACHE; // 清理查询缓存内存碎片。
二、RESET QUERY CACHE; // 从查询缓存中移出全部查询。
三、FLUSH TABLES; //关闭全部打开的表,同时该操做将会清空查询缓存中的内容。
相关文章
相关标签/搜索