mysql-优化启用查询缓存

1、查看是否启用查询缓存

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             | 1048576 |
| query_cache_type             | OFF     |                        #查询缓存没有开启
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
6 rows in set (0.00 sec)

2、修改配置文件开启查询缓存

vi /etc/my.cnf
[mysqld]中添加:
query_cache_size = 500M
query_cache_type = ON

3、重启mysql服务并查看

systemctl restart mysql
mysql> show variables like '%query_cache%'; 
mysql> show status like 'qcache%';                #查看使用情况
mysql> SHOW  GLOBAL STATUS  LIKE  'Qcache%';
+-------------------------+----------+
| Variable_name            | Value   |
+-------------------------+----------+
| Qcache_free_blocks       | 1       | #查询缓存中的空闲块
| Qcache_free_memory       | 16759656| #查询缓存中尚未使用的空闲内存空间
| Qcache_hits              | 16      | #缓存命中次数
| Qcache_inserts           | 71      | #向查询缓存中添加缓存记录的条数
| Qcache_lowmem_prunes     | 0       | #表示因缓存满了而不得不清理部分缓存以存储新的缓存,这样操作的次数。若此数值过大,则表示缓存空间太小了。
| Qcache_not_cached        | 57      | #没能被缓存的次数
| Qcache_queries_in_cache  | 0       | #此时仍留在查询缓存的缓存个数
| Qcache_total_blocks      | 1       | #共分配出去的块数
+-------------------------+----------+

4、衡量缓存是否有效

缓存命中率的计算(次数)

mysql> SHOW GLOBAL STATUS WHERE Variable_name='Qcache_hits' OR Variable_name='Com_select';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| Com_select    | 279292490 | #非缓存查询次数
| Qcache_hits   | 307366973 | # 缓存命中次数
+---------------+-----------
mysql> SHOW GLOBAL STATUS WHERE Variable_name='Qcache_hits' OR Variable_name='Qcache_inserts';
+----------------+-----------+
| Variable_name  | Value     |
+----------------+-----------+
| Qcache_hits | 307416113    | #缓存命中次数
| Qcache_inserts | 108873957 | #向查询缓存中添加缓存记录的条数
+----------------+-----------+

  

  

做一个决定,并不难,难的是付诸行动,并且坚持到底。
原文地址:https://www.cnblogs.com/wukc/p/13612659.html