6

是否可以在 MySQL 查询中使用服务器状态变量?

我可以从“显示状态”中看到各种指标,但如何计算派生值(例如查询缓存命中率)

show global status like 'Qcache_inserts';
show global status like 'Qcache_not_cached';
show global status like 'Qcache_hits';

我如何获得 Qcache_hits/(Qcache_hits+Qcache_inserts+Qcache_not_cached)?

4

2 回答 2

6

通常,您可以通过以下选择访问该信息:

SELECT
   VARIABLE_NAME,
   VARIABLE_VALUE
 FROM
   INFORMATION_SCHEMA.GLOBAL_STATUS

你可以像这样进行你想要的计算:

select (g1.VARIABLE_VALUE / (g1.VARIABLE_VALUE + g2.VARIABLE_VALUE + g3.VARIABLE_VALUE)) as result
FROM INFORMATION_SCHEMA.GLOBAL_STATUS g1
inner join INFORMATION_SCHEMA.GLOBAL_STATUS g2
inner join INFORMATION_SCHEMA.GLOBAL_STATUS g3
where g1.VARIABLE_NAME = 'Qcache_hits'
and g2.VARIABLE_NAME = 'Qcache_inserts'
and g3.VARIABLE_NAME = 'Qcache_not_cached'
于 2012-05-10T10:43:33.463 回答
0

是的你可以。代替“SHOW STATUS”,使用“SELECT VARIABLE_VALUE FROM GLOBAL_STATUS”并像任何其他表一样继续。

该表位于INFORMATION_SCHEMA数据库中。

示例查询:

SELECT VARIABLE_VALUE FROM GLOBAL_STATUS WHERE VARIABLE_NAME="Qcache_inserts"

希望这可以帮助 !

于 2012-05-10T11:04:37.437 回答