mysql-优化启用查询缓存
1、查看是否启用查询缓存
1 2 3 4 5 6 7 8 9 10 11 12 | 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、修改配置文件开启查询缓存
1 2 3 4 | vi /etc/my .cnf [mysqld]中添加: query_cache_size = 500M query_cache_type = ON |
3、重启mysql服务并查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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、衡量缓存是否有效
缓存命中率的计算(次数)
1 2 3 4 5 6 7 | mysql> SHOW GLOBAL STATUS WHERE Variable_name= 'Qcache_hits' OR Variable_name= 'Com_select' ; +---------------+-----------+ | Variable_name | Value | +---------------+-----------+ | Com_select | 279292490 | #非缓存查询次数 | Qcache_hits | 307366973 | # 缓存命中次数 +---------------+----------- |
1 2 3 4 5 6 7 | mysql> SHOW GLOBAL STATUS WHERE Variable_name= 'Qcache_hits' OR Variable_name= 'Qcache_inserts' ; +----------------+-----------+ | Variable_name | Value | +----------------+-----------+ | Qcache_hits | 307416113 | #缓存命中次数 | Qcache_inserts | 108873957 | #向查询缓存中添加缓存记录的条数 +----------------+-----------+ |
做一个决定,并不难,难的是付诸行动,并且坚持到底。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)