1 withQs as(2 SelectCp.objtype asObject_type,/*type*/3 db_name(st.dbid) as [Database],/*Database*/4Object_schema_name (St.objectid,st.dbid) as [Schema],/*Architecture*/5 object_name(st.objectid,st.dbid) as [Object],/*Object Name*/6 Convert(Char( -), Qs.creation_time, -) asPlan_creation,/*Scheduled build time*/7 Convert(Char( -), Qs.last_execution_time, -) asLast_execution,/*Last Execution Time*/8 Qs.plan_generation_num,9Qs.execution_count,/*Number of executions*/TenQs.total_elapsed_time/( +*Qs.execution_count) asAvg_elapesd_seconds,/*Total time spent Ms*/ OneQs.total_worker_time/( +*Qs.execution_count) asAvg_cpu_cost,/*average CPU time consuming Ms*/ AQs.total_logical_reads/Qs.execution_count asAvg_logical_reads,/*average logical Read*/ -Qs.total_logical_writes/Qs.execution_count asAvg_logical_writes,/*average Logical Write*/ -Qs.total_physical_reads/Qs.execution_count asAvg_physical_reads,/*Average room reading*/ theSt.text,/*Execute text*/ -Qp.query_plan/*Execution Plan*/ - fromsys.dm_exec_query_stats QS - JoinSys.dm_exec_cached_plans CP onCp.plan_handle=Qs.plan_handle + CrossApply Sys.dm_exec_sql_text (sql_handle) asSt - CrossApply Sys.dm_exec_query_plan (Qs.plan_handle) asQP +)Select Top - * fromQS A where text like '%%' at --and object_type= ' Proc ' - --and avg_logical_reads>200 - andExecution_count> - /*Number of executions*/ - --and Last_execution_time>dateadd (Mi,-10,getdate ()) - andLast_execution>= '2016-05-01 00:00:00.000' /*Last Execution Time*/ - Order byAvg_cpu_costdesc in
SQL Server time-consuming operation based on execution plan query