对使用sys.dm_exec_query_stats动态视图者的警告(A Warning to Those Using sys.dm_exec_query_stats)
来源:互联网 发布:jsp页面js格式化日期 编辑:程序博客网 时间:2024/04/28 08:08
原文:
http://sqlblog.com/blogs/adam_machanic/archive/2010/04/22/a-warning-to-those-using-sys-dm-exec-query-stats.aspx
Adam Machanic
A Warning to Those Using sys.dm_exec_query_stats
The sys.dm_exec_query_stats view is one of my favorite DMVs. It has replaced a large chunk of what I used to use SQL Trace for--pulling metrics about what queries are running and how often--and it makes this kind of data collection painless and automatic. What's not to love? But use cases for the view are a topic for another post. Today I want to quickly point out an inconsistency.
If you're using this view heavily, as I am, you should know that in some cases your queries will not get a row. One such case, as it turns out, is any time an ALTER TABLE appears in your batch. "No big deal," you might be thinking. "I don't regularly alter tables in stored procedures." But think again. Do you ever create a temporary table using SELECT INTO? Do you ever create indexes on the resultant table? I know I do--and quite often those indexes are primary keys, or unique constraints.
The interesting thing is that CREATE INDEX does not suffer from this problem. You can create unique indexes all day long and still get entries in the query stats DMV. And thus the inconsistency: Using ALTER TABLE to create a primary key or unique constraint on a temporary table is, for all intents and purposes exactly the same thing as creating a unique index using CREATE INDEX. But one works with the query_stats DMV and the other doesn't.
It's not difficult to imagine how this inconsistency arose; just rather annoying in practice. I went through a bunch of code today and changed several instances of ALTER TABLE to CREATE INDEX, and my monitoring scripts are already working better as a result. I recommend that you take the time to do the same, if you use this view.
To conclude this post, a quick repro so that you can see the issue:
SELECT
'abc' AS a
INTO #x
ALTER TABLE #x
ADD UNIQUE (a)
SELECT *
FROM sys.dm_exec_query_stats
WHERE
plan_handle IN
(
SELECT
plan_handle
FROM sys.dm_exec_requests
WHERE
session_id = @@SPID
)
DROP TABLE #x
GO
SELECT
'abc' AS a
INTO #x
CREATE UNIQUE INDEX whatever
ON #x (a)
SELECT *
FROM sys.dm_exec_query_stats
WHERE
plan_handle IN
(
SELECT
plan_handle
FROM sys.dm_exec_requests
WHERE
session_id = @@SPID
)
DROP TABLE #x
GO
Enjoy, and best of luck in all of your monitoring endeavors.
- 对使用sys.dm_exec_query_stats动态视图者的警告(A Warning to Those Using sys.dm_exec_query_stats)
- SQL Server 2005 sys.dm_exec_query_stats
- 用sys.dm_exec_query_stats 动态管理查看查询最耗 IO 资源的 SQL 语法
- sql server数据库占用cpu太大,使用sys.dm_exec_query_stats查询优化
- sql server性能分析--DMV之(sys.dm_exec_query_stats )
- sql server性能分析--DMV之(sys.dm_exec_query_stats ) .
- sys.dm_exec_cached_plans 视图的使用
- 非sys用户下存储过程对v$视图的使用
- 非sys用户下存储过程对v$视图的使用
- WinUsbNet: A managed interface to WinUSB.sys
- oracle10g的系统视图(sys、system)
- sys
- /sys
- sys
- sys
- Python sys.path的使用
- 视图SYS.USER_TAB_COLS SYS.USER_TAB_COLUMNS 区别
- Sqlserver等待事件:动态性能视图:sys.dm_os_wait_stats
- 有一个班,3个学生,各4门课,计算总平均分数以及第n个学生的成绩
- list,set,map,数组间的相互转换
- Qt4.6.0在visual studio 2005中的配置过程详解
- QQ输入账号缓慢 无法登陆 错误码0x00060008
- 串口调试和自动测试工具
- 对使用sys.dm_exec_query_stats动态视图者的警告(A Warning to Those Using sys.dm_exec_query_stats)
- 求助 ! API版HelloWorld
- 诗剑书生的独白
- 批处理取消文件夹隐藏属性
- 在线媒体播放
- MIT技术评论:2010十大新兴技术
- 2010.04.26 IRC 基本命令篇
- 20100426
- FX1S控制步进电机的实例(图与程序)