📜  嵌套循环连接和哈希连接的区别

📅  最后修改于: 2021-09-10 03:01:46             🧑  作者: Mango

1. 嵌套循环连接:
这是一种物理连接算法,用于连接 2 个关系。此连接是一种内部连接技术,这意味着我们无法看到连接。这是所有连接类型中最简单的一种。这是最适合小数据和小交易的算法。在名为 R 和 S 的 2 个关系的情况下,嵌套循环连接的算法如下:

For each record x of R read in, do
Use the index on B for S
Get all the matching records (having B=x.A)
End

2. 哈希连接:
Hash Join 也是一种物理连接算法,用于内部连接两个表。联接是一种内部联接技术,这意味着我们看不到联接。连接选择由查询优化器自动完成。散列连接使用两个步骤执行,构建和探测。在名为 R 和 S 的 2 个关系的情况下,Hash join 的算法如下:

Hash records of R, one by one, using A values
(Use the same M buckets and same hash function h)
Hash matching pair of records into the same bucket
End

嵌套循环连接和哈希连接的区别:

S.No. Nested Loop Join Hash Join
1. It is processed by forming an outer loop within an inner loop after which the inner loop is individually processed for the fewer entries that it has. It is specifically used in case of joining of larger tables.
2. The nested join has the least performance in case of large tables. It has best performance in case of large and sorted and non-indexed inputs.
3. There are two phases in this, outer and inner loop processing. The two phases in this are build and probe.
4. Steps involved include identifying an outer driving table and assigning the inner table to it, and processing the rows of inner table for every outer table row. The steps involved are building a Hash table on a small table. It is used to probe the hash value of the Hash table is applicable for each element in the second row.
5. Index range scan is done here. Full-table scan of the smaller table is done in case of hash join.
6. This uses lesser RAM resources. It uses more RAM resources.
7. It is the most common type of join. It is not as common as the nested loop join.
8. Least number of comparisons are required in case of nested loop join. It needs more comparisons than the nested loop join thereby using more RAM resources.
9. It is the fastest join algorithm due to least number of comparisons. It is not as fast due to more number of comparisons.
10. It is better than all other types of join for small transactions and small data. It is not as good as nested loop join in case of smaller data.
11. It is of three types, namely, nested loop join, indexed nested loop join and Temporary indexed nested loop join. Its types are classic hash join, Grace hash join, hybrid hash join, hash anti join, hash semi-join, recursive hash join and hash bailout.
12. It is not automatically selected. This join is automatically selected in case there is no specific reason to adopt other types of join algorithms. It is also known as the go-to guy of all the join operators.