Configuring caching in MySQL

Configuring caching in MySQL can significantly enhance database performance by reducing query execution times. Below are the steps to configure caching in MySQL:


1. Enable Query Cache

The query cache is used to store the results of SELECT queries. If the same query is executed again, MySQL can return the cached result without re-executing the query.

Steps:
  1. Check if the query cache is enabled:

    SHOW VARIABLES LIKE 'query_cache%';

  2. Enable and configure query cache in the MySQL configuration file (my.cnf or my.ini):

    [mysqld] query_cache_type = 1 # 1 = ON, 2 = DEMAND (only cache queries with SQL_CACHE) query_cache_size = 128M # Set the size of the query cache query_cache_limit = 2M # Maximum size of individual query results to cache

  3. Restart the MySQL service to apply changes:

    sudo service mysql restart

  4. Use SQL_CACHE or SQL_NO_CACHE in queries (if query_cache_type = 2):

    SELECT SQL_CACHE * FROM my_table WHERE id = 1;


2. Use InnoDB Buffer Pool

For the InnoDB storage engine, the InnoDB Buffer Pool is the primary caching mechanism for data and indexes.

Steps:
  1. Configure the buffer pool size in my.cnf:

    [mysqld] innodb_buffer_pool_size = 4G # Set it to about 70-80% of total available memory innodb_buffer_pool_instances = 4 # Number of buffer pool instances (for large buffer sizes)

  2. Restart MySQL:

    sudo service mysql restart

  3. Monitor usage:

    SHOW ENGINE INNODB STATUS\G;


3. Configure Table Cache

The table cache determines how many table handles MySQL can keep open at a time.

Steps:
  1. Configure table_open_cache in my.cnf:

    [mysqld] table_open_cache = 2000 # Adjust based on workload

  2. Monitor open tables:

    SHOW STATUS LIKE 'Open_tables';


4. Use Performance Schema and Query Optimization

MySQL includes a Performance Schema to monitor and optimize queries, which helps identify which queries to cache or optimize.

Steps:
  1. Enable Performance Schema:

    [mysqld] performance_schema = ON

  2. Analyze slow queries:

    SHOW FULL PROCESSLIST;

  3. Enable and configure the slow query log:

    [mysqld] slow_query_log = 1 slow_query_log_file = /var/log/mysql-slow.log long_query_time = 1

  4. Restart MySQL and review slow queries:

    sudo service mysql restart tail -f /var/log/mysql-slow.log


5. Use Proxy Cache (Optional)

Consider using a proxy tool like ProxySQL for advanced query caching mechanisms.


6. Monitor Cache Usage

Use these commands to monitor caching performance:

  • Query cache: SHOW STATUS LIKE 'Qcache%';

  • InnoDB buffer pool: SHOW STATUS LIKE 'Innodb_buffer_pool%';

相关推荐
白帽攻防录5 小时前
SRC 挖洞:Roundcube 预认证 SQL 注入深度复盘,CVE-2026-48842 preg_replace 转义绕过怎么打穿邮件系统
网络·数据库·sql·网络安全·sql注入
꯭自꯭闭꯭5 小时前
达梦SQL优化相关
linux·运维·数据库·sql
nhdh5 小时前
B+树揭秘:MySQL索引核心原理全解析
数据结构·b树·mysql
对空六课6 小时前
支持注意力分析的热力图工具有哪些?
前端·数据库·数据分析
南京码讯光电技术有限公司6 小时前
How to Design an Antenna System for an Industrial WiFi Module
数据库·人工智能
lusklusklusk7 小时前
Oracle数据库基础之2_体系结构
数据库·oracle
可乐ea7 小时前
从第一性原理构建 AI Agent:提示词、工具、技能与记忆全解剖
数据库·人工智能·工具调用·ai智能体·提示词工程·agent开发·智能体记忆
余槐i7 小时前
MySQL 深分页从 4 秒到 60 毫秒:延迟关联减少回表的实测与代价
mysql·性能优化·sql优化·执行计划·慢查询优化
东方护航数据恢复(深圳)8 小时前
医疗案例:HIS/PACS 数据库页损坏修复,医院不停诊完成恢复【东方护航数据恢复深圳店】
数据库·数据恢复·医疗·二次开盘
lusklusklusk9 小时前
Oracle数据库基础之11_Duplicate和Dataguard和RAC
数据库·oracle