IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]如何得到执行最耗时的前N条T-SQL语句--【叶子】

    maco_wang发表于 2012-02-06 14:53:37
    love 0
    --得到最耗时的前N条T-SQL语句
    
    --适用于SQL SERVER 2005及其以上版本
    
    --给N赋初值为30
    declare @n int set @n=30 
    
    ;with maco as 
    (   
        select top (@n)
            plan_handle,
            sum(total_worker_time) as total_worker_time ,
            sum(execution_count) as execution_count ,
            count(1) as sql_count
        from sys.dm_exec_query_stats group by plan_handle
        order by sum(total_worker_time) desc
    )
    select  t.text ,
            a.total_worker_time ,
            a.execution_count ,
            a.sql_count
    from    maco a
            cross apply sys.dm_exec_sql_text(plan_handle) t
            
    /* 结果格式如下
    text     total_worker_time  execution_count   sql_count
    -------- ------------------ ----------------- ---------
    内容略
    */


沪ICP备19023445号-2号
友情链接