with QS as(  
    select cp.objtype as object_type  
          ,db_name(st.dbid)as [database]  
          ,object_schema_name(st.objectid,st.dbid)as [schema]  
          ,object_name(st.objectid,st.dbid)as [object]  
          ,convert(char(16),qs.creation_time,120)as plan_creation  
          ,convert(char(16),qs.last_execution_time,120)as last_execution  
          ,qs.plan_generation_num  
          ,qs.execution_count  
          ,qs.total_worker_time  
          ,qs.total_physical_reads  
          ,qs.total_logical_writes  
          ,qs.total_logical_reads  
          ,qs.total_elapsed_time/(1000000*qs.execution_count)as avg_elapesd_seconds  
          ,qs.total_worker_time/qs.execution_count as avg_cpu_cost  
          ,qs.total_logical_reads/qs.execution_count as avg_logical_reads  
          ,qs.total_logical_writes/qs.execution_count as avg_logical_writes  
          ,qs.total_physical_reads/qs.execution_count as avg_physical_reads  
          ,st.text  
          ,qp.query_plan  
    from sys.dm_exec_query_stats qs  
         join sys.dm_exec_cached_plans cp on cp.plan_handle=qs.plan_handle  
         cross apply sys.dm_exec_sql_text(sql_handle)as st  
         cross apply sys.dm_exec_query_plan(qs.plan_handle)as qp  
    where 1=1  
    --and cp.objtype='Proc'                 --对象类型  
    --and db_name(st.dbid)='GprsRun'        --数据库  
    --and st.text not like '%时间%' and st.text not like '%@queryStr%'        --查询字符串  
    --and qs.execution_count>100         --执行次数  
    --and qs.total_worker_time>100           --cpu总时间      
    --and qs.total_physical_reads>100        --物理读次数  
    --and qs.total_logical_writes>100        --逻辑写次数  
    --and qs.total_logical_reads>100     --逻辑读次数  
)  
 
select *, '执行次数最多的' type from (select top 5 * from QS order by execution_count desc)a               --执行次数最多的  
union all  
select *, '执行时间最长的' type from (select top 5 * from QS order by total_worker_time desc)a             --执行时间最长的  
union all  
select *, '物理读次数最多的' type from (select top 5 * from QS order by total_physical_reads desc)a         --物理读次数最多的  
union all  
select *, '逻辑写次数最多的' type from (select top 5 * from QS order by total_logical_writes desc)a         --逻辑写次数最多的  
union all  
select *, '逻辑读次数最多的' type from (select top 5 * from QS order by total_logical_reads desc)a          --逻辑读次数最多的  
union all  
select *, '平均cpu时间最长的' type from (select top 5 * from QS order by avg_cpu_cost desc)a                   --平均cpu时间最长的  
union all  
select *, '平均逻辑读最多的' type from (select top 5 * from QS order by avg_logical_reads desc)a                --平均逻辑读最多的  
union all  
select *, '平均逻辑写最多的' type from (select top 5 * from QS order by avg_logical_writes desc)a           --平均逻辑写最多的  
union all  
select *, '平均物理写最多的' type from (select top 5 * from QS order by avg_physical_reads desc)a           --平均物理写最多的  
order by text

查询sql执行速度的更多相关文章

  1. 查询SQL执行情况

    /* 查询SQL执行情况 包含逻辑读取信息,执行信息等情况*/ SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total ...

  2. Mysql资料 查询SQL执行顺序

    目录 一.Mysql数据库查询Sql的执行顺序是什么? 二.具体顺序 一.Mysql数据库查询Sql的执行顺序是什么? (9)SELECT (10) DISTINCT column, (6)AGG_F ...

  3. sql运算符优先级及逻辑处理顺序--查询sql执行顺序

    sql逻辑处理顺序 --开启和关闭查询 --SET STATISTICS TIME ON---------------------------------------------请先来看看SET ST ...

  4. Hibernate 模糊查询 ' %?% ' SQL执行异常

    今天我在使用Hibernate 的SQL预编译之后注入参数的形式写了一条模糊查询语句.刚开始我是这么写的

  5. showplan_text查询计划查询 sql执行顺序 时间 IO

    http://www.cnblogs.com/happyday56/archive/2009/09/10/1564144.html   set showplan_text ongoselect exp ...

  6. pgsql SQL监控,查询SQL执行情况

    SELECT procpid, START, now() - START AS lap, current_query FROM ( SELECT backendid, pg_stat_get_back ...

  7. 一条查询sql的执行流程和底层原理

    1.一条查询SQL执行流程图 2.查询SQL执行流程之发送SQL请求 (1)客户端按照Mysql通信协议将SQL发送到服务端,SQL到达服务端后,服务端会单起一个线程执行SQL. (2)执行时Mysq ...

  8. 面试官:说说一条查询sql的执行流程和底层原理?

    一条查询SQL执行流程图如下 序章 自我介绍 我是一条sql,就是一条长长的字符串,不要问我长什么样,因为我比较傲娇. 额~~不是我不说啊,因为细说起来,我可以细分为DML(Update.Insert ...

  9. 【知识点整理】Oracle中NOLOGGING、APPEND、ARCHIVE和PARALLEL下,REDO、UNDO和执行速度的比较

    [知识点整理]Oracle中NOLOGGING.APPEND.ARCHIVE和PARALLEL下,REDO.UNDO和执行速度的比较 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 ...

随机推荐

  1. 如何生成SPFILE文件

    1.spfile是Oracle9i之后引入的,目的是提高系统安全性.在Oracle8i下初始化参数文件为文本文件,可以使用文本编辑器进行编辑,当需要修改初始化参数时,需要在init.ora文件中修改, ...

  2. FineUI与百度地图简单示例 (转帖)

    http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=4191&extra=page%3D1 前台代码 <%@ Page ...

  3. 源码|ThreadLocal的实现原理

    ThreadLocal也叫"线程本地变量"."线程局部变量": 其作用域覆盖线程,而不是某个具体任务: 其"自然"的生命周期与线程的生命周期 ...

  4. PAT 乙级 1038 统计同成绩的学生C++版

    1038. 统计同成绩学生(20) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求读入N名学生的成绩,将 ...

  5. [转]Oracle 中计算时间间隔的SQL 语句

    ' second as TSec from dual -- 计算 60秒 前的时间 ' minute as TMin from dual -- 计算 10分 前的时间 ' hour as UTCTim ...

  6. [转]IE6 的浮动

    IE6 的一个 BUG,(IE6 双倍边距 BUG),只要满足下面 3 个条件才会出现这个 BUG: 1)要为块状元素: 2)要左侧浮动: 3)要有左外边距(margin-left): 解决这个 BU ...

  7. Fix-Dell iDRAC 7 error: RAC0218: The maximum number of user sessions is reached

    Hi Everyone, We came across the following error while performing some preventative maintenance check ...

  8. Fragment之介绍(转)

    http://www.cnblogs.com/plokmju/p/3239265.html 前言 开门见山开篇名义,本篇博客将讲解一下Android中Fragment的内容,必要的地方会提供相应的演示 ...

  9. Notepad++ 删除空白行的方法(转)

    Notepad++ 是我特别喜欢的一款编程工具.在安装后就可以轻松使用了.Notepad++ 上提供了很多方便的插件以实现更多的扩展,当然自身已经比较强大好用了.如果你遇到文本中间有大量的空白行的话, ...

  10. javascript节点操作replaceChild()

    replaceChild(a,b)是用来替换文档中的已有元素的 参数a:要插入的节点, 参数b:要替换的节点 var oDiv = document.getElementById("guoD ...