【问题标题】:sql server logical reads are more with where clause?sql server 逻辑读取更多的是用where子句?
【发布时间】:2013-12-28 14:12:21
【问题描述】:
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.

在第二个查询中,我用O.custid &lt; 5 限制了行,但它仍然给我60 个逻辑读取?客户表中第一次查询显示的应该小于 21。

【问题讨论】:

  • 逻辑读取意义不大。测量经过的时间。 where 子句可能意味着 SQL Server 的工作量更少或更多。

标签: sql sql-server sql-server-2008 sql-server-2008-r2


【解决方案1】:

您需要查看两个查询的查询计划。 SQL Server 有几个用于执行这些查询的选项。例如,这里有一些例子:

  • 扫描Customers 并使用索引查找Orders 中的每个订单。
  • 扫描Orders 表并查找Customers 中的每个客户。
  • OrdersCustomers 创建一个哈希表,然后将结果合并到哈希表中。
  • CustomersOrders 进行排序并进行合并连接。

(当然还有其他可能性)。

在任何情况下,SQL 优化器都会选择最佳方法来执行join。当您添加where 子句时,最佳解决方案可能会有所不同。这是意料之中的,并且表明优化器正在完成它的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2019-05-18
    • 2015-02-28
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多