📜  嵌套循环联接和排序合并联接之间的区别

📅  最后修改于: 2021-08-24 17:01:50             🧑  作者: Mango

1.嵌套循环连接:
嵌套循环连接是最简单的连接算法,由于涉及的比较次数较少,因此通常比所有其他类型的连接具有更好的性能。将外部表中的每一行与内部表中的每一行进行比较。用于两个关系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.排序合并联接:
如果是已排序的关系,则排序合并联接是最快的联接。此联接是内部联接,例如嵌套循环联接。无法看到或实现该算法,我们只能提示引擎使用此算法。该算法由合并和排序阶段组成,分别用于合并和排序操作。下面给出了两个表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.