Explain Nested join in oracle SQL.

 


Introduction:

It is also called as the nested loop join. It is called as type of join algorithm which is used to retrieve the data from the two or more tables. For every row of the outer table is matched with the inner table. Inner table is called as the child table where the outer table is called as the parent table. One table is accessed through an index lookup for each row which is retrieved from another table.

A row in the outer table is selected at once time and matched with every row of the outer table.All the rows which are present in the inner table are scanned as many times as the number of rows in the outer table which is done by an optimiser.It provides an analysis of the statistics of the table.If there is no indexes,




Why should we use?

  • When one of the tables is small or indexed and having the few rows.
  • When the indexes are available on the join keys of the inner table and join condition allows for an efficient access to the inner table.
  • The join conditions are highly selective with small subset of data is joined.             
  • OLTP transactional system.
  •  Queries which include the ordering or leading hints.
  •  When the outer table is small.
  •  When an Optimizer is set to first_rows which is used to retrieve the first few rows quickly.


select e.ename,d.dname
  from emp e
  join dept d
  on e.deptno=d.deptno;

Oracle may use the nested loop join like if there is an index on emp.deptno & dept.deptno which is very efficient.Here,emp is the outer table and dept is the inner table.

  create index ui_dept on dept(deptno);
  
    create index ui_emp on emp(deptno);

With the above queries,ui_dept and ui_emp are the index name which are created on the table dept and emp.

EXPLAIN PLAN FOR
select e.ename,d.dname
  from emp e
  join dept d
  on e.deptno=d.deptno;



  select * from table(DBMS_XPLAN.display);



Characteristics in terms of performance:

Advantages:

It is very efficient for the small datasets.

Low overhead occurs for starting the returning rows.

It is considered as good when the driving table or outer table has few rows.

Disadvantages:

It can be inefficient for the larger tables without the proper indexing.

Performance will be degrading if an inner table is accessed many times.

Conclusion:
  • If the outer table is large and the inner table is not indexed efficiently, it can become inefficient.
  • It is efficient when the outer table is small, and the inner table is indexed efficiently.
  • The database starts by reading the first row from the outer table.
  • For each and every row of the outer table, the database checks all the rows in the inner table to satisfy the join condition.
  • If there is match, the corresponding rows from both the tables are combined & the resulting row is added to the result set.
  • This process repeats for each remaining row in the outer table.

Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

Explain oracle pl sql datatypes interview question and answer for fresher and experienced.

Difference between full join and cross join-oracle sql plsql