怎么查找执行比较慢的sql语句-DBA给的建议

  • 1.使用sql动态视图

如下:

select top 10 b.text,a.total_worker_time,a.total_logical_reads,a.total_elapsed_time,execution_count
from sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) as b
order by total_worker_time/execution_count desc 

查看联机帮助文档视图:sys.dm_exec_query_stats可得到所有字段含义,主要有

total_worker_time:总的cpu消耗时间

total_logical_reads:逻辑读次数

total_elapsed_time:耗时

execution_count:执行次数。

total_worker_time/execution_count desc 表示每次执行需要消耗多少cpu,按照倒叙排序,找到最消耗cpu的sql语句。text

 

上面只可以查找SqlServer清空缓存之后的数据,如果SqlServer重启过,或者SqlServer由于内存不足导致执行计划被释放。那么是无法找到的。

  • 2.使用攻击profiler。新建跟踪:

勾选以下事件。第一个是存储过程,第二个是sql语句

筛选器选择超过100秒的为慢sql(单位为毫秒),点击确定。如下:

 

posted @ 2015-04-16 21:40  cctext  阅读(1683)  评论(0编辑  收藏  举报