📜  嵌套循环连接和排序合并连接的区别

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

1. 嵌套循环连接:
嵌套循环联接是最简单的联接算法,由于涉及的比较次数较少,因此通常比所有其他类型的联接具有更好的性能。外部表中的每一行都与内部表中的每一行进行比较。 2 个关系 R 和 S 的嵌套循环连接算法:

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.排序合并连接:
在排序关系的情况下,排序合并连接是最快的连接。此连接是类似于嵌套循环连接的内部连接。该算法无法看到或实现,我们只能提示引擎使用该算法。该算法由合并和排序阶段组成,分别用于合并和排序操作。 2 个表 R 和 S 的排序合并连接算法如下:

If R is sorted on A, S is sorted on B do
Merge R and S to get join result
End

嵌套循环连接和排序合并连接的区别:

S.No. Nested Loop Join Sort Merge 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 usually used to join two independent sources of data represented in a table.
2. The nested join has the least performance in case of large tables. It is better than nested join in case of performance in large tables.
3. There are two phases in this, outer and inner loop processing. It consists of 2 phases consisting the sort operation and merge operation.
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 first row from the first table and second row from the table is taken, if it is not the end then, the selected rows are checked for the merger. If they can be merged, the merged row is returned else next rows are taken from the tables and the steps are repeated until the rows are exhausted.
5. It is not as fast as sort merge join in case of sorted tables. It is the fastest join operation in case of sorted tables. This is because it uses merge phase and sort phase, where, if sort is already previously done, then merge is the fastest operation.
6. It is of three types, namely, nested loop join, indexed nested loop join and Temporary indexed nested loop join. It does not have further classifications.
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 (generally).
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.