码迷,mamicode.com
首页 > 其他好文 > 详细

计划缓存的查询和统计

时间:2015-10-15 23:24:54      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

一,查看计划缓存

当一个查询被优化器用于执行查询或操作数据时,产生的执行计划会被缓存到plan cache中,可以通过 DMV:sys.dm_exec_cached_plans 查看被缓存的执行计划等对象。

DMV:sys.dm_exec_cached_plans

Returns a row for each query plan that is cached by SQL Server for faster query execution. You can use this dynamic management view to find cached query plans, cached query text, the amount of memory taken by cached plans, and the reuse count of the cached plans.

 

这个dmv返回的重要字段

size_in_bytes Number of bytes consumed by the cache object.

cacheobjtype : Type of object in the cache,主要是Compiled Plan

objtype : Type of object,主要是Proc(Stored procedure),Adhoc(Ad hoc query).

plan_handle  Identifier for the in-memory plan. This identifier is transient and remains constant only while the plan remains in the cache. This value may be used with DMF : sys.dm_exec_sql_text 和  sys.dm_exec_query_plan 。

通过Plan_Handle字段和DMF:sys.dm_exec_query_plan  来查看预估的执行计划,在SSMS中可以通过XML转换为图形界面;通过Plan_Handle字段和DMF:sys.dm_exec_sql_text  来查看缓存的sql 查询语句。

 

示例

1,清空计划缓存

DBCC FREEPROCCACHE

2,执行查询语句,生成计划缓存

select *
from dbo.dt_test
where id >5

3,查看计划缓存

select cp.size_in_bytes,cp.cacheobjtype,cp.objtype,cp.usecounts,
    st.dbid,st.objectid,st.text,
    qp.query_plan
from sys.dm_exec_cached_plans cp 
cross APPLY sys.dm_exec_sql_text(cp.plan_handle) as st
cross APPLY sys.dm_exec_query_plan(cp.plan_handle) as qp 
where cp.objtype=Adhoc
    and st.dbid=db_id()

 技术分享

二,计划缓存的统计信息 

通过 DMV:sys.dm_exec_query_stats 来查看计划缓存的统计信息

Returns aggregate performance statistics for cached query plans in SQL Server.The view contains one row per query statement within the cached plan, and the lifetime of the rows are tied to the plan itself.When a plan is removed from the cache, the corresponding rows are eliminated from this view.

这个DMV中有几个重要的字段:总的CPU工作时间,总的占用时间和执行次数

total_worker_timeTotal amount of CPU time, reported in microseconds (but only accurate to milliseconds), that was consumed by executions of this plan since it was compiled.For natively compiled stored procedures, total_worker_time may not be accurate if many executions take less than 1 millisecond.

total_elapsed_timeTotal elapsed time, reported in microseconds (but only accurate to milliseconds), for completed executions of this plan.

execution_countNumber of times that the plan has been executed since it was last compiled.

 

根据被缓存的执行计划找到数据库中耗时最长的20个查询语句

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 

SELECT TOP 20 
    CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2)) AS [Total Duration (s)] ,
    CAST(qs.total_worker_time * 100.0 / qs.total_elapsed_time AS DECIMAL(28, 2)) AS [% CPU] ,
    CASt(qs.total_worker_time/ 1000000.0/qs.execution_count as DECIMAL(28,2)) as [Avg CPU Time(s)],
    CAST((qs.total_elapsed_time - qs.total_worker_time)* 100.0 / 
         qs.total_elapsed_time AS DECIMAL(28, 2)) AS [% Waiting] ,
    qs.execution_count , 
    CAST(qs.total_elapsed_time / 1000000.0 / qs.execution_count AS DECIMAL(28, 2)) AS [Average Duration (s)] ,
    SUBSTRING (qt.text,(qs.statement_start_offset/2) + 1,      
     ((CASE WHEN qs.statement_end_offset = -1 
       THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2 
       ELSE qs.statement_end_offset 
       END - qs.statement_start_offset)/2) + 1) AS [Individual Query ],
    qt.text AS [Parent Query] ,
    DB_NAME(qt.dbid) AS DatabaseName ,
    qp.query_plan 
 FROM sys.dm_exec_query_stats qs 
 CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt 
 CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp 
 WHERE qs.total_elapsed_time > 0 
 ORDER BY qs.total_elapsed_time DESC     

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

 

参考文章:

https://msdn.microsoft.com/zh-cn/library/ms187404.aspx

https://msdn.microsoft.com/ZH-CN/LIBRARY/ms189741

https://msdn.microsoft.com/en-us/library/ms189747.aspx

http://www.cnblogs.com/CareySon/archive/2013/05/04/PlanCacheInSQLServer.html

 

计划缓存的查询和统计

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4883621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!