27.2. Buffering and Caching

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

查看缓存是否开启

		
MySQL> select @@query_cache_type;
MySQL> show variables like 'query_cache_type';
		
		

开启与关闭缓存

		
MySQL> set query_cache_type=on;
MySQL> set query_cache_type=off;
		
		

查看缓存状态

show variables like 'have_query_cache';
		

查询缓存的大小

		
MySQL> select @@global.query_cache_size;
MySQL> select @@query_cache_size;
		
		

查看最大缓存限制,如果集大于该数则不缓存。

		
MySQL> select @@global.query_cache_limit;
		
		

清除缓存/重置缓存

		
MySQL> flush tables;
MySQL> flush query cache;
MySQL> reset query cache;
		
		

查询缓存性能

		
MySQL> show status like 'qcache%';

MySQL> show status like 'qcache_q%';
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| Qcache_queries_in_cache | 1 |
+-------------------------+-------+
1 row in set (0.00 sec)

MySQL> show status like 'qcache_f%';
+--------------------+----------+
| Variable_name | Value |
+--------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16766728 |
+--------------------+----------+
2 rows in set (0.00 sec)
		
		

27.2.1. Query Cache SELECT Options

Two query cache-related options may be specified in SELECT statements:

SQL_CACHE

The query result is cached if it is cacheable and the value of the query_cache_type system variable is ON or DEMAND.

SQL_NO_CACHE

The query result is not cached.

Examples:

SELECT SQL_CACHE id, name FROM customer;
SELECT SQL_NO_CACHE id, name FROM customer;


SELECT /*! SQL_NO_CACHE */ stuff FROM table

			

例 27.1. SQL_CACHE 测试

下面的例子中你将看到缓存变化

				
flush tables;
show status like 'qcache_q%';
select sql_cache * from members limit 5;
show status like 'qcache_q%';
select sql_cache * from members limit 10;
show status like 'qcache_q%';
				
				

显示当前缓存中的信息数量:

				
MySQL> show status like 'qcache_q%';
				
				
其中各个参数的意义如下:
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:缓存中块的数量。				
				





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
存储 缓存 Java
yii\caching\CacheInterface 接口是干什么的?底层原理是什么?
yii\caching\CacheInterface 接口是干什么的?底层原理是什么?
|
存储 缓存 JSON
Caching(三)|学习笔记
快速学习 Caching(三)
167 0
Caching(三)|学习笔记
|
存储 算法 NoSQL
Caching(二)|学习笔记
快速学习 Caching(二)
134 0
Caching(二)|学习笔记
|
存储 缓存 NoSQL
Caching(一)|学习笔记
快速学习 Caching(一)
156 0
Caching(一)|学习笔记
|
缓存 Java 数据库
@Caching&@ CacheConfig |学习笔记
快速学习 @Caching&@ CacheConfig
113 0
|
存储 Web App开发 缓存
Client Side Cache 和 Server Side Cache 的区别
Client Side Cache 和 Server Side Cache 的区别
186 0
Client Side Cache 和 Server Side Cache 的区别
|
缓存 前端开发 rax
x86系统cache locking的原理
x86系统cache locking的原理
318 0
|
NoSQL PHP Redis
WordPress Caching Solutions Part 1 – Performance Benchmarking and Installing Redis Object Caching
In previous tutorials I took you through the steps to set up a new server instance, then get your WordPress sites up and running, including transactional emails.
2444 0
|
缓存
shiro之cache问题
错误原因分析加解决方案,以供大家参考: 1、错误信息:net.sf.ehcache.ObjectExistsException: Cache shiro-activeSessionCache already exists 翻译过来的意思是:缓存shiro- activesoncache已经存在。
2460 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等