循环加入 SQL Server 2008

Loop Join in SQL Server 2008

我不清楚下面提到的查询之间的工作差异。

Specifically I'm unclear about the concept of OPTION(LOOP JOIN).

第一种方法:这是使用的传统连接,比以下所有方法都昂贵。

SELECT * 
FROM [Item Detail] a
LEFT JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (FORCE ORDER);

第二种方法:它在一个语句中包含 OPTION 排序数据,只是优化。

SELECT * 
FROM [Item Detail] a
LEFT LOOP JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (FORCE ORDER);

第三种方法: 在这里,我不清楚查询是如何工作的,包括 OPTIONloop join!!?

SELECT * 
FROM [Item Detail] a
LEFT LOOP JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (LOOP JOIN);

谁能解释一下彼此之间的差异和工作方式以及优势?

注意:这些是不是嵌套或哈希循环!

来自 Query Hints (Transact-SQL)

FORCE ORDER Specifies that the join order indicated by the query syntax is preserved during query optimization. Using FORCE ORDER does not affect possible role reversal behavior of the query optimizer.

还有

{ LOOP | MERGE | HASH } JOIN Specifies that all join operations are performed by LOOP JOIN, MERGE JOIN, or HASH JOIN in the whole query. If more than one join hint is specified, the optimizer selects the least expensive join strategy from the allowed ones.

Advanced Query Tuning Concepts

If one join input is small (fewer than 10 rows) and the other join input is fairly large and indexed on its join columns, an index nested loops join is the fastest join operation because they require the least I/O and the fewest comparisons.

If the two join inputs are not small but are sorted on their join column (for example, if they were obtained by scanning sorted indexes), a merge join is the fastest join operation.

Hash joins can efficiently process large, unsorted, nonindexed inputs.

Join Hints (Transact-SQL)

Join hints specify that the query optimizer enforce a join strategy between two tables

您的选项 1 告诉优化器保持连接顺序不变。所以 JOIN 类型可以由优化器决定,所以可能是 MERGE JOIN.

您的选项 2 告诉优化器针对此特定 JOIN 使用 LOOP JOIN。如果 FROM 部分中有任何其他连接,优化器将能够为它们做出决定。此外,您还指定了优化器采用的 JOINS 顺序。

您的最后一个选项 OPTION (LOOP JOIN) 将对查询中的所有联接强制实施 LOOP JOIN

综上所述,优化器很少会选择不正确的计划,这可能表明存在更大的潜在问题,例如过时的统计信息或碎片化的索引。