Explain merge join in oracle SQL .
Introduction:
Merge join in Oracle SQL PLSQL topic is a method which is used to join the two sorted datasets efficiently. It is effective when both the input tables are large. It is also called as sort merge join. It sorts both the input sets i.e. tables or subqueries which is already sorted on the join key columns if they are not already sorted.
Here, input datasets means that tables or subqueries .In the other words, it is the type of join operation which is used by the oracle optimizer which combine the rows from the two sorted datasets which is based on the join condition by using an equi join(=).Sorted data is merged like the zipper.In this,memory usuage is medium.
How it works?
It checks if the data from the two tables which is already sorted on the join keys. If not, it will sort them.
Both the input tables or the result sets are sorted on the join key.
Matching rows which are joined and returned.
When it is used?
Both the inputs of the tables are large and can be sorted efficiently.
If there is an index or order by which already sorts the data.
When the hash join is not optimal and the nested loop join would be slow due to the large volumes.
When the join is an equi join.
Both the tables are large.
Sorting is cheap or already done due to indexes and previous operations.
It is used when the join column are sorted by the range indexes.
select e.empno,d.dname
from emp e
JOIN dept d
ON e.deptno=d.deptno;
If emp and dept are both sorted by the deptno, oracle may use the merge join.
Data is retrieved in the sorted manner with the help of indexes or no sorting is needed & merge the two sorted datasets efficiently.
Summary:
It minimizes the repeated scans which is by leveraging the sorted order.
When indexes or pre-sorted data will reduce the sorting overhead.
Optimizer automatically select when the cost estimates Favour over an alternative.
Advantages:
It is efficient for large and sorted datasets.
It works for equality and range conditions.
After sorting linear merge phase.
Memory or disk is required for sorting.
It is not ideal for the small tables.
Sorting the unsorted data includes overhead.
Comments
Post a Comment