MySQL的query缓存

query cache的配置

MySQL查询缓存用于缓存用户的查询结果,这就是为什么两次同样的查询,第一次慢的话,第二次会非常快。

的配置项有三个:

query_cache_type = 1
query_cache_limit   = 1M
query_cache_size    = 16M

query_cache_type有三种取值,0/OFF表示关闭,1/ON表示开启,但不包含那些以SELECT SQL_NO_CACHE打头的查询,2表示仅仅缓存那些以SELECT SQL_CACHE打头的查询。

query_cache_limit表示单条查询结果超过这一大小,就不缓存。

query_cache_size表示缓存空间大小。

查询相关配置变量为:

> show variables like 'query%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| query_alloc_block_size       | 16384    |
| query_cache_limit            | 1048576  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 16777216 |
| query_cache_strip_comments   | OFF      |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
| query_prealloc_size          | 24576    |
+------------------------------+----------+

特别注意:因为全局的缓存是各处理线程共享的,所以如果开启了该缓存,处理时会需要先争得mutex,缓存结果时也需要争得mutex,故而,每个SQL查询都会带来一点点额外开销。

query cache的状态

查询MySQL当前的查询结果缓存状态为:

> show status like 'qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 45       |
| Qcache_free_memory      | 13693064 |
| Qcache_hits             | 72552    |
| Qcache_inserts          | 20393    |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 7707     |
| Qcache_queries_in_cache | 220      |
| Qcache_total_blocks     | 592      |
+-------------------------+----------+

Qcache_free_blocks 表示内存块数,缓存空间的长期拆分分配会带来块数上升,也就是碎片化上升。Qcache_inserts 表示历史总共向查询缓存里添加了多少次需要缓存的结果。Qcache_lowmem_prunes 表示因为内存不够而删除缓存的次数,有可能是分配的内存不足,也可能是内存太碎片化。

flush query cache命令只会整理内存碎片,但并不会删除缓存中的查询结果,如下Qcache_free_blocks明显下降,但Qcache_queries_in_cache未变:

> flush query cache;
> show status like 'qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 2        |
| Qcache_free_memory      | 13693728 |
| Qcache_hits             | 72552    |
| Qcache_inserts          | 20415    |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 7707     |
| Qcache_queries_in_cache | 220      |
| Qcache_total_blocks     | 548      |
+-------------------------+----------+

reset query cache命令才是用来清空缓存中的查询结构的,如下Qcache_queries_in_cache归零:

> Reset Query  Cache;
> show status like 'qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 16759656 |
| Qcache_hits             | 72552    |
| Qcache_inserts          | 20421    |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 7707     |
| Qcache_queries_in_cache | 0        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+

 

发表于 2018年08月24日 15:08   评论:0   阅读:2203  



回到顶部

首页 | 关于我 | 关于本站 | 站内留言 | rss
python logo   django logo   tornado logo