mysql 查询语句统计和查询缓存相关的状态值解疑答惑

阅读: 评论:0

mysql 查询语句统计和查询缓存相关的状态值解疑答惑

mysql 查询语句统计和查询缓存相关的状态值解疑答惑

关于mysql里‘show global status’输出的Com_select、Queries、Questions、Qcache_hits、Qcache_inserts、Qcache_not_cached这几个状态值一直有些迷惑,现通过实验来更加准确和深刻的了解之。

实验版本:5.5.39

先附上每个状态的官方解释

Com_select:The Com_xxx statement counter variables indicate the number of times each xxx statement hasbeen executed.However, if a query result is returned from query cache, the server increments the Qcache_hits status variable, not Com_select.

select语句执行的次数,如果结果从查询缓存里得来,就增加Qcache_hits,而不增加Com_select

Queries:The number of statements executed by the server. This variable includes statements executed within stored programs, unlike the Questions variable. It does not count COM_PING or COM_STATISTICS commands.

server执行过的所有语句,计数包含存储存储程序(存储过程)里面的sql条数

Questions:The number of statements executed by the server. This includes only statements sent to the server by clients and not statements executed within stored programs, unlike the Queries variable. This variable does not count COM_PING, COM_STATISTICS, COM_STMT_PREPARE, COM_STMT_CLOSE, or COM_STMT_RESET commands.

只包含由client发过来的sql语句数量(包括use语句,show语句等),不包括存储过程里的sql语句(一个存储过程作为一条语句)

Qcache_hits:The number of query cache hits.

查询缓存命中的次数

Qcache_inserts:The number of queries added to the query cache.

加入到查询缓存的次数

Qcache_not_cached:The number of noncached queries (not cacheable, or not cached due to the query_cache_type setting).

不可缓存的查询数量。不可缓存的(例如结果集大小超过query_cache_limit的);或者由于query_cache_type设置(on  允许、off  不允许、demand  依赖明确指定的使用缓存)不能使用查询缓存的数量。

Qcache_queries_in_cache:The number of queries registered in the query cache

在查询缓存中‘注册’过的查询语句的条数


下面通过实验来观察个 状态值的增长情况:

重启数据库,清空查询缓存

执行一条查询语句,然后观察个状态值

Com_select    | 1

Qcache_hits   | 0

Qcache_inserts  | 1

在执行上面执行的查询语句,个状态值

Com_select 不变

Qcache_hits 加1

Qcache_inserts 不变

明确指定不使用缓存(加上SQL_NO_CACHE),执行刚才的查询语句

Com_select 加1

Qcache_hits 不变

Qcache_not_cached 加1


在搞清楚了以上几个值的含义后,讨论一下计算‘查询缓存命中率’的公式,关于查询缓存命中率的计算方法,网上说法不一,这里说一下我的理解:

查询语句分为‘可缓存’和‘不可缓存’两种

可缓存的:若存在于query_cache中,则Com_select不变,Qcache_hits加1

          不存在于query_cache中,则Com_select加1,Qcache_inserts加1

不可缓存的:select加1,Qcache_not_cached加1

即 Com_select = Qcache_inserts+Qcache_not_cached

所以查询缓存的命中率可以计算如下:

Qcache_hits/(Qcache_hits+Com_select)


附:查询缓存变量含义:


Qcache_free_blocks

目前还处于空闲状态的 Query Cache中内存Block 数目,数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。

Qcache_free_memory

缓存中的空闲内存总量。

Qcache_hits

缓存命中次数。

Qcache_inserts

缓存失效次数(查询结果新增进QueryCache的数量)

Qcache_lowmem_prunes

缓存出现内存不足并且必须要进行清理以便为Qcache_inserts动作腾出空间的次数,这个数字最好长时间来看;如果这个数字增长较快,就表示可能碎片非常严重,或者内存很少。(上面的free_blocks和free_memory可以告诉您属于哪种情况)。

Qcache_not_cached

不适合进行缓存的查询的数量,通常是由于这些查询不是SELECT语句以及由于query_cache_type设置的不会被Cache的查询,或者结果集超过query_cache_limit变量设置的语句。

Qcache_queries_in_cache

当前在query_cache中‘注册’的select语句条数

Qcache_total_blocks

缓存中块的总量。





     本文转自kai404 51CTO博客,原文链接:,如需转载请自行联系原作者


本文发布于:2024-02-04 23:46:55,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170719141160861.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缓存   语句   状态   mysql
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23