用一条语句查看一段时间内status变量的变化

简介: The InnoDB plugin has a nice INFORMATION_SCHEMA concept: resetting tables. For example, the INNODB_CMP table lists information about compression operation.

The InnoDB plugin has a nice INFORMATION_SCHEMA concept: resetting tables. For example, the INNODB_CMP table lists information about compression operation. A similar table, INNODB_CMP_RESET, provides the same information, but resets the values. The latter can be used to measure, for example, number of compression operations over time.

I wish to present a SQL trick which does the same, without need for resetting tables. Suppose you have some status table, and you wish to measure the change in status per second, per minute etc. The trick is to query for the value twice in the same query, with some pause in between, and make the difference calculation.

For sake of simplicity, I’ll demonstrate using 5.1’s INFORMATION_SCHEMA.GLOBAL_STATUS. Please refer to INFORMATION_SCHEMA.GLOBAL_STATUS: watch out for some discussion on this.

In our example, we wish to measure the number of questions per second. Getting the number of questions is done with:

SELECT * FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME = 'questions';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| QUESTIONS     | 3619           |
+---------------+----------------+
1 row in set (0.00 sec)

Applying the trick, thus solving the problem:

SELECT SUM(value) AS questions_per_sec FROM (
SELECT -VARIABLE_VALUE AS value
FROM INFORMATION_SCHEMA.GLOBAL_STATUS
WHERE VARIABLE_NAME = 'questions'
UNION ALL
SELECT SLEEP(1)
FROM DUAL
UNION ALL
SELECT VARIABLE_VALUE
FROM INFORMATION_SCHEMA.GLOBAL_STATUS
WHERE VARIABLE_NAME = 'questions'
) s1;
+-------------------+
| questions_per_sec |
+-------------------+
|               126 |
+-------------------+
1 row in set (1.01 sec)

Make a one minute measurement with SLEEP(60), then divide SUM by 60.

Note on transactional tables

The above trick will not work when reading values from transactional tables, and with isolation level >= REPEATABLE-READ, since, by definition, you must get the same value back while in the same transaction. So this works on MyISAM, MEMORY, functions and otherwise non transactional data sources.

相关文章
|
Serverless
函数计算在执行请求的过程中遇到了意外的错误
函数计算在执行请求的过程中遇到了意外的错误
97 1
|
Java 数据库连接 mybatis
mybatismybatisPlus Update操作返回值不是影响行数
mybatismybatisPlus Update操作返回值不是影响行数
699 0
|
7月前
|
关系型数据库 MySQL 测试技术
当update修改数据与原数据相同时会再次执行吗
当update修改数据与原数据相同时会再次执行吗
52 1
|
Go 数据库
sync.Once-保证运行期间的某段代码只会执行一次
sync.Once-保证运行期间的某段代码只会执行一次
88 0
|
7月前
|
Python
改变循环执行的状态
在编程中,循环是常用的控制结构之一,它允许代码块在一定条件下重复执行。然而,有时候我们可能需要在循环执行过程中改变其状态,比如提前终止循环或跳过当前迭代进入下一次迭代。Python提供了break和continue语句,帮助我们改变循环的执行状态。下面,我们将详细介绍如何使用这两个语句来改变循环的执行状态,并附上示例代码。
63 0
|
关系型数据库 MySQL Java
MySQL 巨坑:update 更新慎用影响行数做判断!!!
MySQL 巨坑:update 更新慎用影响行数做判断!!!
1361 0
MySQL 巨坑:update 更新慎用影响行数做判断!!!
|
JavaScript
清除JS定时器setInterval方法防止if条件语句判断无效的解决方案
清除JS定时器setInterval方法防止if条件语句判断无效的解决方案
154 0
|
关系型数据库 MySQL 测试技术
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
196 0
|
SQL 数据库连接 数据库
实时错误“91” 对象变量或with块变量未设置”
实时错误“91” 对象变量或with块变量未设置”
853 0
实时错误“91” 对象变量或with块变量未设置”