[英]sql server logical reads are more with where clause?
SELECT C.custid, C.companyname,
O.orderid, O.orderdate
FROM Sales.Customers AS C
INNER JOIN Sales.Orders AS O
ON C.custid = O.custid
(830 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 'Orders'. Scan count 1, logical reads 21, 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 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SELECT C.custid, C.companyname,
O.orderid, O.orderdate
FROM Sales.Customers AS C
INNER JOIN Sales.Orders AS O
ON C.custid = O.custid
WHERE O.custid < 5
(30 row(s) affected)
Table 'Customers'. Scan count 0, logical reads 60, 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 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
In second query I'm limiting the rows with O.custid < 5
still it gives me logical reads 60? 在第二个查询中,我限制了
O.custid < 5
的行, O.custid < 5
仍然使我逻辑读取60? It should be less than 21 which shown for first query in customer table. 客户查询表中的第一个查询应小于21。
This is too long for a comment. 这个评论太长了。
You need to look at the query plans for the two queries. 您需要查看两个查询的查询计划。 SQL Server has several options for performing these queries.
SQL Server有几个用于执行这些查询的选项。 For instance, here are some examples:
例如,下面是一些示例:
Customers
and looking up each order in Orders
using an index. Customers
并使用索引查找订单中的每个Orders
。 Orders
table and looking up each customer in Customers
. Orders
表,并在每个客户查找Customers
。 Orders
and Customers
and then merging the results in the hash table. Orders
和Customers
创建哈希表,然后将结果合并到哈希表中。 Customers
and Orders
and doing a merge join. Customers
和Orders
进行排序并进行合并联接。 (and there are undoubtedly other possibilities as well). (无疑还有其他可能性)。
In any case, the SQL optimizer chooses the best method for doing the join
. 无论如何,SQL优化器都会选择最佳方法来执行
join
。 When you add a where
clause, the best solution may be different. 添加
where
子句时,最佳解决方案可能有所不同。 This is expected, and an indication that the optimizer is doing its job. 这是预料之中的,并且表明优化器正在执行其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.