1.了解query_cache
QueryCache:MySQL缓存机制,就是SELECT语句的执行结果集。它是以KV的形式存储在缓存区,KV 分别指SELECT语句和查询结果集。当执行查询SQL时,不再经过Optimizer模块进行解析与优化,直接从缓存中捞数据。
query_cache是查询缓存,我们可以通过其参数来查看mysql的查询缓存相关的东西,如下图所示:
上图中命令show variables like ‘%query_cache%’;
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.01 sec)
Ⅰ.query_cache_limit 设置缓存的单条查询结果集的最大容量,查询结果集如果超过该值将不被缓存;
Ⅱ. query_cache_min_res_unit 设置每个缓存最小占用空间
Ⅲ. query_cache_size 设置缓存所使用的内存大小,必须是1024的整数倍,如果不是会自动被降低至1024的倍数
Ⅳ. query_cache_type 控制缓存机制的开关.可填值0(禁用)/1(启用)/2(demand)demand 也是启用query_cache ,但是select后需要加sql_cache才生效。
Ⅴ. query_cache_wlock_invalidate 控制写锁定时是否失效缓存,可填值1(true)在写锁定的同时失效缓存,0(false)写锁定时仍读取该对象缓存
Ⅵ. query_cache_wlock_invalidate:控制当有写锁定发生在表上的时刻是否先失效该表相关的Query Cache,如果设置为 1(TRUE),则在写锁定的同时将失效该表相关的所有Query Cache,如果设置为0(FALSE)则在锁定时刻仍然允许读取该表相关的Query Cache。
2、配置启用缓存机制
etc目录下,配置my.cf文件,添加配置项query_cache_type=1即可。配置完成后需重启mysql:service mysql restart。配置示意图如下:
之后,我们实际演示一下,先看一下原始的缓存命中状态
mysql> show global status like 'QCache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 41925840 |--空闲的内存
| Qcache_hits | 0 |--查询缓存命中的次数
| Qcache_inserts | 0 |--新插入的缓存次数
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+----------+
接着select语句第一次查询, Qcache_free_memory会变小,因为缓存占用了部分导致; Qcache_inserts增加,因为是首次查询。
mysql> select * from test_a;
+------+------+
| id1 | id2 |
+------+------+
| 1 | 1 |
| 2 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> show global status like 'QCache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 41924304 |
| Qcache_hits | 0 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+-------------------------+----------+
8 rows in set (0.00 sec)
然后我们重复查询一次上面的sql,观察变化
mysql> select * from test_a;
+------+------+
| id1 | id2 |
+------+------+
| 1 | 1 |
| 2 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> show global status like 'QCache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 41924304 |
| Qcache_hits | 1 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+-------------------------+----------+
可以多重复查询几次,观察Qcache_hits命中的次数,会更好理解。