我正在学习SQL Server 2008执行计划并进行测试我已经运行了两个查询并尝试比较结果。
第一个查询是否比第二个查询运行得更快?我通常会检查执行成本(性能和成本)。
首次质询
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 parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(931 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 'Visits'. Scan count 2, logical reads 1235, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Assignments'. Scan count 1, logical reads 4657, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 203 ms, elapsed time = 198 ms.
第二次查询
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
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.
(1 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 'Visits'. Scan count 1, logical reads 19335, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Assignments'. Scan count 1, logical reads 4657, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 109 ms, elapsed time = 250 ms.
答案 0 :(得分:1)
根据统计数据输出,第一个更快。在那里寻找的东西包括经过的时间和读/写的数量。
第一次查询:
SQL Server Execution Times: CPU time = 203 ms, elapsed time = 198 ms.
第二次查询:
SQL Server Execution Times: CPU time = 109 ms, elapsed time = 250 ms.
第一个查询的执行速度稍快一些。毫秒可能看起来微不足道,但考虑到每天运行数千次的查询,所有这些微小的秒数将开始快速加起来。
要寻找的另一件事是读取和写入(这将代表为获取结果集而必须完成的工作量。
第一次查询:
(931 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 'Visits'. Scan count 2, **logical reads 1235**, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Assignments'. Scan count 1, logical reads 4657, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
第二次查询:
(1 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 'Visits'. Scan count 1, **logical reads 19335**, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Assignments'. Scan count 1, logical reads 4657, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Logical reads
表示从缓冲区缓存中检索的数据,这比从physical reads
便宜,其中数据是从物理磁盘中检索的(这是昂贵的)。不过,越少越好。
在这种情况下,您的第一个查询在较短的时间内运行(经过时间= 198ms
与250ms
),并且需要的读取次数少于第二个查询,(1235
vs {{1因此,优先于第二个。
执行计划中的另一个提示是,更粗的条表示传递的数据越多。然而,虽然他们可以给你一个直观的线索,但一定要验证它的结果,因为我经历过显示的执行计划不准确的情况。