跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

MySQL 查询缓存的实际应用代码示例_MySQL

MySQL查询 以下的文章主要介绍的是 mysql 查询缓存的实际应用代码以及查看mysql 查询缓存的大小 ,碎片整理,清除缓存以及监视mysql 查询缓存性能的相关内容的描述,以下就是具体内容的描述,希望在你今后的学习中会有所帮助。
  1. MySQL> select @@query_cache_type;
  2. +--------------------+
  3. | @@query_cache_type |
  4. +--------------------+
  5. | ON |
  6. +--------------------+
  7. MySQL> set query_cache_type=off;
  8. MySQL> set query_cache_type=on;
  9. MySQL>
  10. MySQL> select sql_cache id, title, body from article;
  11. MySQL> select sql_no_cache id, title, body from article;
  12. MySQL> show variables like 'have_query_cache';
  13. +------------------+-------+
  14. | Variable_name | Value |
  15. +------------------+-------+
  16. | have_query_cache | YES |
  17. +------------------+-------+
  18. 1 row in set (0.00 sec)
MySQL(Linux) MySQL 9.6.0是面向Linux平台的2026年创新版本,核心架构迎来重大革新。其将外键约束与级联操作从InnoDB引擎层上移至SQL层,确保所有数据变更均被完整记录至Binlog,彻底解决了CDC(变更数据捕获)与主从复制中的数据不一致难题。此外,该版本引入container_aware启动选项以原生适配容器环境,并对审计日志进行了组件化重构,为追求极致数据一致性与云原生体验的开发者提供了全新选择。 下载 查看MySQL 查询缓存的大小
  1. MySQL> select @@global.query_cache_size;
  2. +---------------------------+
  3. | @@global.query_cache_size |
  4. +---------------------------+
  5. | 16777216 |
  6. +---------------------------+
  7. 1 row in set (0.00 sec)
  8. MySQL> select @@query_cache_size;
  9. +--------------------+
  10. | @@query_cache_size |
  11. +--------------------+
  12. | 16777216 |
  13. +--------------------+
  14. 1 row in set (0.00 sec)
查看最大缓存结果,如果结果集大于该数,不缓存。
  1. MySQL> select @@global.query_cache_limit;
  2. +----------------------------+
  3. | @@global.query_cache_limit |
  4. +----------------------------+
  5. | 1048576 |
  6. +----------------------------+
  7. 1 row in set (0.00 sec)
碎片整理
  1. MySQL> flush query cache
  2. -> ;
  3. Query OK, 0 rows affected (0.00 sec)
清除缓存
  1. MySQL> reset query cache
  2. -> ;
  3. Query OK, 0 rows affected (0.00 sec)
监视MySQL 查询缓存性能:
  1. MySQL> flush tables;
  2. Query OK, 0 rows affected (0.04 sec)
  3. MySQL> show status like 'qcache%';
  4. +-------------------------+----------+
  5. | Variable_name | Value |
  6. +-------------------------+----------+
  7. | Qcache_free_blocks | 1 |
  8. | Qcache_free_memory | 16768408 |
  9. | Qcache_hits | 6 |
  10. | Qcache_inserts | 36 |
  11. | Qcache_lowmem_prunes | 0 |
  12. | Qcache_not_cached | 86 |
  13. | Qcache_queries_in_cache | 0 |
  14. | Qcache_total_blocks | 1 |
  15. +-------------------------+----------+
  16. 8 rows in set (0.06 sec)
看看当前缓存中有多少条信息:
  1. MySQL> show status like 'qcache_q%';
  2. +-------------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------------+-------+
  5. | Qcache_queries_in_cache | 0 |
  6. +-------------------------+-------+
  7. 1 row in set (0.00 sec)
  8. MySQL> select sql_cache id, title, body from article;
  9. MySQL> show status like 'qcache_q%';
  10. +-------------------------+-------+
  11. | Variable_name | Value |
  12. +-------------------------+-------+
  13. | Qcache_queries_in_cache | 1 |
  14. +-------------------------+-------+
  15. 1 row in set (0.00 sec)
  16. MySQL> show status like 'qcache_f%';
  17. +--------------------+----------+
  18. | Variable_name | Value |
  19. +--------------------+----------+
  20. | Qcache_free_blocks | 1 |
  21. | Qcache_free_memory | 16766728 |
  22. +--------------------+----------+
  23. 2 rows in set (0.00 sec)
以上的相关内容就是对MySQL 查询缓存的介绍,望你能有所收获。

相关文章