Man*_*jot 8 performance sql-server-2005
我想找出导致我在性能监视器计数器中看到的高 SQL 编译(而不是重新编译)的原因。
这是我的看法:如果我看到很多 SQl 编译,那么这意味着我们系统上的查询没有被缓存,原因如下:
许多即席查询
运行 SQL 不缓存的查询,例如:
UPDATE table1 SET col1='String 长度超过 8000 个字符.....' WHERE key_column = some int
计划超时并从缓存中删除,因为: 缓存空间不足或计划使用时间不够长。
在探查器中捕获缓存插入的唯一方法是存储过程->SP:CacheInserts,但它只关注存储过程缓存。
所以我尝试了以下方法来获取临时查询:
SELECT [cp].[refcounts] -- when Refcounts becomes 0, plan is excluded from cache.
, [cp].[usecounts]
, [cp].[objtype]
, 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 ) st
CROSS APPLY sys.dm_exec_query_plan ( cp.plan_handle ) qp ;
Run Code Online (Sandbox Code Playgroud)
我认为导致编译的查询应该是 objtype= Adhoc 的查询,但这也可能与重新编译有关。现在我必须运行分析器,捕获导致重新编译的查询,然后将其从上面的列表中排除。
我是否朝着正确的方向前进?
是否可以使用单个查询来实现 SQL 编译而无需太多工作?
帮助我获得上述知识的资源:
http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/954b4fba-3774-42e3-86e7-e5172abe0c83
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=143946
http: //technet.microsoft.com/en-nz/library/cc966425(en-us).aspx
http://www.sqlservercentral.com/Forums/Topic914951-360-1.aspx
任何帮助都非常感谢。
我不认为您可以通过简单的方式找到它,但无论如何都有可能解决这个问题。Profiler 提供了许多可用于分析查询性能的事件类类型。启动一个新的 Profiler 会话并检查以下事件:
Performance: Performance statistics
Stored Procedures: RPC:Completed
TSQL: SQL:BatchCompleted
TSQL: SQL: BatchStarting
Run Code Online (Sandbox Code Playgroud)
选中显示所有列并选择性能下的每一列:仅性能统计事件。其余事件可以保留默认设置。
接下来,选择列过滤器并按 DatabaseName 和/或 LoginName/ApplicationName/HostName 等过滤,如果您知道它们。目的是限制在 Profiler 中显示的行数,并仅关注您的需求。
接下来,按 Run 让它运行一段时间(2-3 分钟,只要你需要)。分析显示的结果主要看:性能统计事件。
如果性能统计经常出现,则意味着查询的计划被第一次缓存、编译、重新编译或从 PlanCache 中驱逐。据我所知,如果查询在计划缓存中没有其查询计划 - 您将看到 2 行PerformanceStatistics事件,然后是SQL:BatchStarting,然后是SQL:BatchCompleted。这意味着查询计划首先被编译、缓存,然后查询开始并完成。
查看性能统计事件下的以下列:
SPID - ID of the session on which the event occurred. You can use it to identify the
row on SQL:BatchCompleted event which will display the SQL Query text and other
usefull information (Read/Writes, StartTime/EndTime)
Duration - Total time, in microseconds, spent during compilation.
EventSubClass - 0 = New batch SQL text that is not currently present in the cache.
1 = Queries within a stored procedure have been compiled.
2 = Queries within an ad hoc SQL statement have been compiled.
3 = A cached query has been destroyed and the historical performance
data associated with the plan is about to be destroyed.
4 = A cached stored procedure has been removed from the cache and the
historical performance data associated with it is about to be
destroyed.
5 = A cached trigger has been removed from the cache and the historical
performance data associated with it is about to be destroyed.
Run Code Online (Sandbox Code Playgroud)
考虑到 EventSubClass 编号,您可以了解查询计划发生了什么并采取具体措施。此外,如果您对 HostName、WindowsUser 或 Profiler 跟踪中的其他信息感兴趣,您可以将其他列添加到存储过程和TSQL事件类。此外,跟踪可以存储在 SQL 表中,使分析更容易,更可定制。这是一个描述更多性能统计事件类的链接。
归档时间: |
|
查看次数: |
5099 次 |
最近记录: |