我还在学习在SQL Server中阅读执行计划。我想知道是否有人可以告诉我以下哪个查询更快。
左连接:
SELECT
[InvoiceNum] = CONVERT(VARCHAR(10),H.Date, 101) + ' #'
+ RTRIM(SUBSTRING(RptText, CHARINDEX('No.', RptText) + 3, 25))
FROM
dbo.RptDisplayHeader H
LEFT JOIN
dbo.RptDisplayDetail d ON d.RptID =H.RptID
AND d.ID =H.ID
AND d.Date =H.Date
AND d.Seq = 2
WHERE
H.ID = @ID
ANDH.RptID = @RptID
ORDER BY
H.Date DESC;
内联查询:
SELECT
[InvoiceNum] = CONVERT(VARCHAR(10),H.Date, 101)
+ (SELECT
' #' + RTRIM(SUBSTRING(RptText, CHARINDEX('No.', RptText) + 3, 25))
FROM
dbo.RptDisplayDetail d
WHERE
d.ID = @ID
AND d.RptID = @RptID
AND d.Seq = 2
AND d.Date =H.Date)
FROM
dbo.RptDisplayHeaderH
WHERE
H.ID = @ID
ANDH.RptID = @RptID
ORDER BY
H.Date DESC;
执行计划:
内联统计:
Table 'RptDisplayDetail'. Scan count 1723, logical reads 5444, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'RptDisplayHeader'. Scan count 1, logical reads 58, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
左连接统计信息:
Table 'RptDisplayDetail'. Scan count 1723, logical reads 5792, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'RptDisplayHeader'. Scan count 1, logical reads 58, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
答案 0 :(得分:0)
您需要比较最左边的select语句的Estimated Sub Tree成本(这是最后执行的操作)
成本最低的那个(理论上)是您最快的查询。
更可靠的衡量标准是您发布的IO统计信息。
根据您发布的统计信息,内联查询略有改善,因为RptDisplayDetail表上的逻辑读取较少。但是,就个人而言,我会取消这种边际收益,支持其他查询的可读性(除非您特别遇到性能问题)
有a great video over at BrentOzar.com在其中调整StackOverflow数据库上的示例查询