sql-server - 如果一个查询具有较低的逻辑读取但另一个查询具有较低的相对查询成本,则哪个查询执行得更好?
问题描述
我有两个查询给出相同的结果。
第一个查询:
Select c1.CustomerID, o1.CustomerID
From Customers As c1
Left Join Orders As o1 On c1.CustomerID = o1.CustomerID
Where o1.CustomerID Is Null
第二个查询:
Select c1.CustomerID, Null As CustomerID
From Customers As c1
Where c1.CustomerID Not In (Select CustomerID From Orders)
两者都返回相同的结果。但是时间和 IO 的统计数据是不同的。
> 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 = 10 ms, elapsed time = 10 ms.
>
> (2 rows affected) Table 'Workfile'. 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 '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
> 'Orders'. Scan count 1, logical reads 22, physical reads 0, read-ahead
> reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead
> reads 0. Table 'Customers'. Scan count 1, logical reads 5, physical
> reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads
> 0, lob read-ahead reads 0.
>
> (1 row affected)
>
> SQL Server Execution Times: CPU time = 0 ms, elapsed time = 180
> ms.
>
> (2 rows affected) Table 'Orders'. Scan count 1, logical reads 510,
> physical reads 0, read-ahead reads 0, lob logical reads 0, lob
> physical reads 0, lob read-ahead reads 0. Table 'Customers'. Scan
> count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob
> logical reads 0, lob physical reads 0, lob read-ahead reads 0.
>
> (1 row affected)
>
> SQL Server Execution Times: CPU time = 0 ms, elapsed time = 150
> 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.
以下是查询计划:
第一次查询的相对查询成本为 62%,第二次查询的相对查询成本为 38%。但是,第一种情况下的逻辑读取是 22+5 = 27,而第二种情况是 515。
那么哪个查询被认为表现更好?
解决方案
推荐阅读
- angular - 如何更改 PayPal 按钮区域设置?
- jquery - 如何避免看到数据表的加载延迟?
- json - Flutter 从 REST API 解析 .JSON
- angular - 使用 ngx-extended-pdf-viewer 不显示 Pdf
- java - Java min() 方法不返回数组中的最小值
- typescript - 通过编译器 api 将 typescript 模块编译为纯 web javascript
- c++ - CV_OCL_RUN 宏如何在 OpenCV(版本 3.4.5)的 goodFeaturesToTrack 实现中工作?
- ios - 从 UIViewController 更改 SwiftUI 文本
- python - 在 Django 表单中输入有效的日期/时间
- c - 为什么在 for 循环之后字符数组会保持不变?