SQL Server: 利用 SET STATISTICS IO 和 SET STATISTICS TIME 对T-SQL语句进行性能分析

我们可以用 SET STATISTICS IO 和 SET STATISTICS TIME 对T-SQL语句进行性能分析。

方法如下:


在SQL Server查询分析器里面,执行:

SET STATISTICS profile ON
SET STATISTICS io ON
SET STATISTICS time ON

GO

-- // The SQL statement to analysis

GO

将“-- // The SQL statement to analysis”替换成我们要实际分析的SQL语句即可。


下面贴两张截图来展示一下执行的结果:

SQL Server: 利用 SET STATISTICS IO 和 SET STATISTICS TIME 对T-SQL语句进行性能分析_第1张图片


SQL Server: 利用 SET STATISTICS IO 和 SET STATISTICS TIME 对T-SQL语句进行性能分析_第2张图片


下面我们来详细看一下Messages:


SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
SQL Server parse and compile time: 
   CPU time = 218 ms, elapsed time = 223 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

(5 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblPanel'. Scan count 1, logical reads 78, physical reads 3, read-ahead reads 78, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblVersion'. Scan count 1, logical reads 26, physical reads 2, read-ahead reads 24, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblLayer'. Scan count 5, logical reads 25, physical reads 8, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblControls'. Scan count 5, logical reads 30, physical reads 13, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonControl'. Scan count 5, logical reads 30, physical reads 10, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonState'. Scan count 5, logical reads 30, physical reads 9, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonCommand'. Scan count 1, logical reads 11383, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(22 row(s) affected)

   CPU time = 125 ms,  elapsed time = 509 ms.

这些信息对我们分析查询的性能十分有用。

尤其是:

一、CPU执行时间

 SQL Server Execution Times:
   CPU time = 125 ms,  elapsed time = 509 ms.

我们看到,elapsed time是一个相对比较大的值,因为数据库此刻正忙,背后还可能在执行许多其他的任务,但CPU time则体现出了在当前我们分析的SQL语句中CPU所花费的时间。基本上对于相同的查询、相同的数据库,无论数据库服务器此刻忙碌与否,CPU time是一个相对比较稳定的时间,可以用来衡量SQL语句真实的CPU时间消耗。


二、I/O消耗

见以下信息:

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblPanel'. Scan count 1, logical reads 78, physical reads 3, read-ahead reads 78, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblVersion'. Scan count 1, logical reads 26, physical reads 2, read-ahead reads 24, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblLayer'. Scan count 5, logical reads 25, physical reads 8, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblControls'. Scan count 5, logical reads 30, physical reads 13, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonControl'. Scan count 5, logical reads 30, physical reads 10, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonState'. Scan count 5, logical reads 30, physical reads 9, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tblButtonCommand'. Scan count 1, logical reads 11383, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(22 row(s) affected)


更详细的解释见这篇文章:

《sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME---解释比较详细》http://blog.csdn.net/leamonjxl/article/details/6639978



你可能感兴趣的:(SQL,Server)