mysql查询缓存打开命令_MySql | 查询缓存笔记

环境版本

mysql: 5.6.25

查询缓存

查询缓存是从 4.1 版本开始支持,默认是关闭的,可以在运行时设置变量开启,也可以重写配置文件中的参数开启,两种方法都可行。

开启方法

在 my.cnf 文件中添加 query_cache_type=1

服务运行时 set query_cache_type=1

参数介绍

参数

解释

have_query_cache

当前版本是否支持查询缓存功能

query_cache_limit

允许 Cache 的单条 Query 结果集的最大容量,默认是 1MB,超过此参数设置的 Query 结果集将不会被 Cache

query_cache_min_res_unit

每次分配内存的最小空间大小,也就是每个 Query 的 Cache 最小占用的内存空间大小

query_cache_size

使用的内存大小,默认值为 16M,大小必须是 1024 的整数倍,如果不是整数倍,MySQL 会自动调整降低最小量以达到 1024 的倍数

query_cache_type

控制 Query Cache 功能的开关,可以设置为0(OFF), 1(ON) 和 2(DEMAND) 三种:

query_cache_wlock_invalidate

控制当有写锁加在表上的时候,是否先让该表相关的 Query Cache 失效

补充

query_cache_type 值选项

解释

0(OFF)

关闭 Query Cache 功能,任何情况下都不会使用 Query Cache

1(ON)

开启 Query Cache 功能,但是当 SELECT 语句中使用的 SQL_NO_CACHE 提示后,将不使用 Query Cache

2(DEMAND)

开启 Query Cache 功能,但是只有当 SELECT 语句中使用了 SQL_CACHE 提示后,才使用 Query Cache

query_cache_wlock_invalidate 值选项

解释

1(TRUE)

在写锁定的同时将使该表相关的所有 Query Cache 失效

0(FALSE)

在锁定时刻仍然允许读取该表相关的 Query Cache

初始准备

查看变量

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 | 1 |

| Qcache_free_memory | 16759656 |

| Qcache_hits | 0 |

| Qcache_inserts | 0 |

| Qcache_lowmem_prunes | 0 |

| Qcache_not_cached | 2 |

| Qcache_queries_in_cache | 0 |

| Qcache_total_blocks | 1 |

+-------------------------+----------+

8 rows in set (0.01 sec)

参数解释

参数

解释

Qcache_free_blocks

目前还处于空闲状态的 Query Cache 中内存 Block 数目

Qcache_free_memory

目前还处于空闲状态的 Query Cache 中内存 Block 数目

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 数量

实践检验

动作 1:执行查询

mysql> select * from auth_user where id=1;

状态 1

mysql> show status like '%Qcache%';

+-------------------------+----------+

| Variable_name | Value |

+-------------------------+----------+

| Qcache_free_blocks | 1 |

| Qcache_free_memory | 16757640 |

| Qcache_hits | 0 |

| Qcache_inserts | 1 |

| Qcache_lowmem_prunes | 0 |

| Qcache_not_cached | 3 |

| Qcache_queries_in_cache | 1 |

| Qcache_total_blocks | 4 |

+-------------------------+----------+

8 rows in set (0.00 sec)

动作 2:执行查询

mysql> select * from auth_user where id=1;

状态 2

mysql> show status like '%Qcache%';

+-------------------------+----------+

| Variable_name | Value |

+-------------------------+----------+

| Qcache_free_blocks | 1 |

| Qcache_free_memory | 16757640 |

| Qcache_hits | 1 |

| Qcache_inserts | 1 |

| Qcache_lowmem_prunes | 0 |

| Qcache_not_cached | 3 |

| Qcache_queries_in_cache | 1 |

| Qcache_total_blocks | 4 |

+-------------------------+----------+

8 rows in set (0.00 sec)

动作3:清理查询缓存

mysql> reset query cache;

Query OK, 0 rows affected (0.00 sec)

状态 3

mysql> show status like '%Qcache%';

+-------------------------+----------+

| Variable_name | Value |

+-------------------------+----------+

| Qcache_free_blocks | 1 |

| Qcache_free_memory | 16759656 |

| Qcache_hits | 1 |

| Qcache_inserts | 1 |

| Qcache_lowmem_prunes | 0 |

| Qcache_not_cached | 3 |

| Qcache_queries_in_cache | 0 |

| Qcache_total_blocks | 1 |

+-------------------------+----------+

8 rows in set (0.00 sec)

总结

动作 1 执行后,Qcache_inserts 更新为 1,即动作 1 被缓存,此时 Qcache_hits 为 0

动作 2 执行后,因为是同样的查询动作,所以此时 Qcache_inserts 不变,而 Qcache_hits 更新为 1 ,因为同样的查询动作被命中

动作 3 是清理查询缓存

强烈建议:如果业务中没有大量的相同查询,请关闭查询缓存功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值