mysql系列三、mysql开启缓存、设置缓存大小、缓存过期机制
一、开启缓存
开启缓存,设置缓存大小,具体实施如下:
1、修改配置文件my.ini
windows下是my.ini,linux下是my.cnf;
在配置文件的最后追加上:
- query_cache_type = 1
- query_cache_size = 600000
需要重启mysql生效;
2、命令方式
那么采用第二种方式;
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缓存查询和设置的介绍,望你能有所收获。
二、查看是否生效
1、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 = '';
这样 当我们执行 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;
2、have_query_cache 设置查询缓存是否可用
- mysql> show variables like 'have_query_cache';
- +------------------+-------+
- | Variable_name | Value |
- +------------------+-------+
- | have_query_cache | YES |
上面的显示,表示设置查询缓存是可用的。
3、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 |
- +---------------------------+
显示我们设置新的大小,表示设置成功。
4、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)
5、查看缓存的状态
- 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 数量
6、检查查询缓存使用情况
检查是否从查询缓存中受益的最简单的办法就是检查缓存命中率
当服务器收到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%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。
三、缓存使用策略和过期机制
1、缓存条件
- SHOW STATUS LIKE 'Qcache_hits';
另外即使完全相同的SQL,如果使用不同的字符集、不同的协议等也会被认为是不同的查询而分别进行缓存。
2、缓存数据失效时机
3、缓存清理
mysql系列三、mysql开启缓存、设置缓存大小、缓存过期机制的更多相关文章
- {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句
MySQL数据库初识 MySQL数据库 本节目录 一 数据库概述 二 MySQL介绍 三 MySQL的下载安装.简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 ...
- MySQL强人“锁”难《死磕MySQL系列 三》
系列文章 一.原来一条select语句在MySQL是这样执行的<死磕MySQL系列 一> 二.一生挚友redo log.binlog<死磕MySQL系列 二> 前言 最近数据库 ...
- MySQL系列(三)--数据库结构优化
良好的数据库逻辑设计和物理设计是数据库高性能的基础,所以对于数据库结构优化是很有必要的 数据库结构优化目的: 1.减少数据的冗余 2.尽量避免在数据插入.删除和更新异常 例如:有一张设计不得当的学生选 ...
- 查询事件状态,mysql查看事件是否开启,设置启动时自动开启方法
1.查看事件是否开启 SHOW VARIABLES LIKE 'event_scheduler' 2.设置当前事件开启 SET GLOBAL event_scheduler = 1; 或 SET GL ...
- Mysql系列三:Centos6下安装Mysql和Mysql主从复制的搭建
一.Centos6下安装Mysql 检测下系统有没有自带的mysql:yum list installed | grep mysql, 如果已经有的话执行命令yum -y remove mysql-l ...
- Mvc4页面缓存设置Cookie导致缓存失效
[OutputCache(Duration = 60, VaryByParam = "none")] public ActionResult Index() ...
- 转载-MySQL binlog三种模式及设置方法
原文地址:http://www.cnblogs.com/yangliheng/p/6187327.html 1.1 Row Level 行模式 日志中会记录每一行数据被修改的形式,然后在slave端 ...
- MySQL系列(二)--MySQL存储引擎
影响数据库性能的因素: 1.硬件环境:CPU.内存.存盘IO.网卡流量等 2.存储引擎的选择 3.数据库参数配置(影响最大) 4.数据库结构设计和SQL语句 MySQL采用插件式存储引擎,可以自行选择 ...
- Mysql系列二:Mysql 开发标准规范
原文链接:http://www.cnblogs.com/liulei-LL/p/7729983.html 一.表设计 1. 库名.表名.字段名使用小写字母,“_”分割. 2. 库名.表名.字段名不超过 ...
随机推荐
- LOJ 2664. 「NOI2013」向量内积 解题报告
#2664. 「NOI2013」向量内积 两个 \(d\) 维向量 \(A=[a_1, a_2 ,...,a_d]\) 与 \(B=[b_1 ,b_2 ,...,b_d]\) 的内积为其相对应维度的权 ...
- 【bzoj2229】 Zjoi2011—最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=2229 (题目链接) 题意 给出一张无向图,$q$组询问,每次询问最小割不大于$c$的点对数量. So ...
- 迅雷thunder://协议解密
echo -n 'thunder://''Cg==' | sed 's?thunder://??' | base64 -d | sed 's/^AA//; s/ZZ$//' 将thunder://替换 ...
- A1038. Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- Servlet学习:(三)Servlet3.0 上传文件
转: Servlet学习:(三)Servlet3.0 上传文件 2018年08月03日 11:57:58 iDark_CSDN 阅读数:362 一.注意事项 客户端(浏览器) 表单的提交方法必须是 ...
- 集合类(常见的集合类:Collection、List、Set、ArrayList、linkedList、Vector、HashSet、TreeSet)
一.集合类 定义:一种为了对多个对象进行操作而进行存储的方式. 1.与数组的区别: 数组:可以存储对象,也可以存储基本数据类型,但是一次只能存储一种类型,数组长度固定. 集合:只能存储对象,长度可变, ...
- 【codeforces】【Round#523D】TV shows
题意:n个节目,每个节目的播放时间为[li,ri],你需要选择一些电视机全部播放这些节目,一台电视机不能同时播放多个节目,选择一个新的电视机代价为x , 如果某台电视机的使用时间为[Li,Ri]需要付 ...
- winreg模块的使用
python有内置的注册表操作库--winreg(在33版本中为winreg,在2x版本为_winreg). 1.打开键,枚举键值 # -*- coding: gbk -*- import winre ...
- Spring(2)
前言:控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依 ...
- vue2.0 之文本渲染-v-html、v-text
vue2.0 之文本渲染-v-html.v-text 1.index.html代码 <!DOCTYPE html> <html> <head> <meta c ...