MySQL 查询缓存已启用但未被使用

MySQL 查询缓存已启用但未被使用

我已检查查询缓存已启用

mysql> SHOW VARIABLES LIKE 'have_query_cache';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| have_query_cache | YES   |
+------------------+-------+
1 row in set (0.00 sec)

但似乎没有被使用

mysql> SHOW STATUS LIKE 'Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 16759648 |
| Qcache_hits             | 0        |
| Qcache_inserts          | 0        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 21555882 |
| Qcache_queries_in_cache | 0        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+
8 rows in set (0.00 sec)

任何原因?

答案1

系统变量查询缓存类型也需要设置为非零,因为缓存的存在并不意味着它可以被使用。

答案2

还请注意,并非所有查询都是可缓存的。

例如,如果查询包含 NOW() 之类的函数,则不会被缓存。请查看哪些查询无法缓存的详细说明:https://dev.mysql.com/doc/refman/5.7/en/query-cache-operation.html

答案3

您还应该检查在配置文件中将您的设置写入了哪个位置。

我的问题是,我将 qchace 设置放在了文件末尾。虽然执行后设置已经生效,但mysql> SHOW STATUS LIKE 'Qcache%';实际上并没有缓存任何查询。

将设置放在文件中的不同位置并重新启动 mysql 服务器后,它就可以正常工作了。

在这里您可以看到我的 MySQL 5.6 配置:

[mysqld]
bind-address=*
#skip-networking
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

query_cache_type = 1
query_cache_size =256M
thread_concurrency=2
sort_buffer_size=25M
key_buffer=128M

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

答案4

有一个MySQL 5.6 错误,在 5.6.9 中已修复:“ if the db name (or table name) has a '-' (minus), it does not work”,仅适用于 InnoDB。

相关内容