简体   繁体   English

sql server逻辑读取更多与where子句?

[英]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: 例如,下面是一些示例:

  • Scanning Customers and looking up each order in Orders using an index. 扫描Customers并使用索引查找订单中的每个Orders
  • Scanning Orders table and looking up each customer in Customers . 扫描Orders表,并在每个客户查找Customers
  • Creating a hash table for Orders and Customers and then merging the results in the hash table. OrdersCustomers创建哈希表,然后将结果合并到哈希表中。
  • Sorting both Customers and Orders and doing a merge join. CustomersOrders进行排序并进行合并联接。

(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM