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%';

相关推荐
老一岁3 分钟前
Redis事务_锁_持久化_主从哨兵
linux·数据库·redis
PaperData21 分钟前
1970-2025年省级二氧化碳排放量面板数据
数据库
RestCloud1 小时前
ETLCloud中不同表名之间的数据库数据同步
数据库·etl·数据处理·etlcloud·数据传输·数据集成平台
少陽君2 小时前
如何在 PVE 中下载并导入 Ubuntu 24.04 Server 云镜像
linux·数据库·ubuntu
扑火的小飞蛾4 小时前
生产级SOP:MySQL二进制安装与多实例部署实战(5.6/5.7/8.0/8.4全版本)
数据库·mysql·adb
Pniubi4 小时前
数据迁移里的四个核心位点:分清持久、实时与一次性水位
数据库·binlog·迁移
小张成长计划..4 小时前
【Linux】44:五种IO模型与非阻塞IO
java·服务器·数据库
这里是北京5 小时前
SQL 窗口函数 LAST_VALUE 取错值?从设备巡检报表看懂 ROWS 与 RANGE
数据库·sql
箓维5 小时前
MySQL_表的约束
android·数据库·mysql
GuWenyue5 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端