MySQL缓存使用

查看查询缓存情况

mysql> show variables like ‘%query_cache%’;
(query_cache_type 为 ON 表示已经开启)
±-----------------------------±---------+
| Variable_name | Value |
±-----------------------------±---------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 20971520 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
±-----------------------------±---------+

如果query_cache_type不是ON,修改配置文件以开启查询缓存

root@node01> vim /etc/my.cnf
[mysqld]中添加:
query_cache_size = 20M
query_cache_type = ON

重启mysql服务

root@node01> service mysql restart

查看缓存使用情况

mysql> show status like ‘qcache%’;
±------------------------±---------+
| Variable_name | Value |
±------------------------±---------+
| Qcache_free_blocks | 83 |
| Qcache_free_memory | 19811040 |
| Qcache_hits | 3108196 |
| Qcache_inserts | 757254 |
| Qcache_lowmem_prunes | 20720 |
| Qcache_not_cached | 47219 |
| Qcache_queries_in_cache | 47 |
| Qcache_total_blocks | 276 |
±------------------------±---------+

其中各个参数的意义如下:

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:缓存中块的数量。 

如果不适用缓存查询,使用SQL_NO_CACHE:

select SQL_NO_CACHE count(*) from users where email = ‘hello’;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值