WEB开发网
开发学院数据库MSSQL Server SQL Server性能调教系列(4)--Profiler(下) 阅读

SQL Server性能调教系列(4)--Profiler(下)

 2010-09-27 00:00:00 来源:WEB开发网   
核心提示: b. 用每个签名的检验和计算运行时间填充临时表#AggQueries,包括运行时间的百分比,SQL Server性能调教系列(4)--Profiler(下)(5),以及运行时间降序的行号,IFOBJECT_ID('tempdb..#AggQueries')ISNOTNULLDRO

b. 用每个签名的检验和计算运行时间填充临时表#AggQueries,包括运行时间的百分比,以及运行时间降序的行号。

IF OBJECT_ID('tempdb..#AggQueries') IS NOT NULL 
 DROP TABLE #AggQueries; 
GO 
 
SELECT cs, SUM(duration) AS total_duration, 
 100. * SUM(duration) / SUM(SUM(duration)) OVER() AS pct, 
 ROW_NUMBER() OVER(ORDER BY SUM(duration) DESC) AS rn 
INTO #AggQueries 
FROM dbo.Workload 
GROUP BY cs; 
 
CREATE CLUSTERED INDEX idx_cl_cs ON #AggQueries(cs);

查询聚合之后临时表的内容,数据量会大大的减少,包含签名,总的运行时间,运行时间占总运行时间的半分比,排序序号。

c.筛选并匹配,使用APPLY运算符得到查询模式和一个示例查询。

WITH RunningTotals AS 
( 
 SELECT AQ1.cs, 
  CAST(AQ1.total_duration / 1000. 
   AS DECIMAL(12, 2)) AS total_s, 
  CAST(SUM(AQ2.total_duration) / 1000. 
   AS DECIMAL(12, 2)) AS running_total_s, 
  CAST(AQ1.pct AS DECIMAL(12, 2)) AS pct, 
  CAST(SUM(AQ2.pct) AS DECIMAL(12, 2)) AS run_pct, 
  AQ1.rn 
 FROM #AggQueries AS AQ1 
  JOIN #AggQueries AS AQ2 
   ON AQ2.rn <= AQ1.rn 
 GROUP BY AQ1.cs, AQ1.total_duration, AQ1.pct, AQ1.rn 
 HAVING SUM(AQ2.pct) - AQ1.pct <= 90 -- percentage threshold 
) 
SELECT RT.rn, RT.pct, S.sig, S.tsql_code AS sample_query 
FROM RunningTotals AS RT 
 CROSS APPLY 
  (SELECT TOP(1) tsql_code, dbo.fn_SQLSigCLR(tsql_code) AS sig 
   FROM dbo.Workload AS W 
   WHERE W.cs = RT.cs) AS S 
ORDER BY RT.rn;

4. 有了查询模式,示例查询,和占用时间的百分比例和排序。然后就可以着手优化。也可以通过类似的方式,找到造成大量结果集,大多数的I/O问题的查询模式。

四:总结

Perfiler是一个很好用的工具来追踪系统的性能和工作的负荷,从而准确的找到值得优化的SQL,提高效率,大大减少工作量。

附件下载:

Server性能计数器.rar:http://files.cnblogs.com/changbluesky/Server%e6%80%a7%e8%83%bd%e8%ae%a1%e6%95%b0%e5%99%a8.rar

PerformanceMonitor.rar:http://files.cnblogs.com/changbluesky/PerformanceMonitor.rar

上一页  1 2 3 4 5 

Tags:SQL Server 性能

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接