免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1879 | 回复: 0
打印 上一主题 下一主题

用一条语句查看一段时间内status变量的变化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-01 16:27 |只看该作者 |倒序浏览

                       
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.
                       
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/90603/showart_2083978.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP