Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which table is considered 'inner' in a nested loop join

Can anyone tell me which table is considered to be the inner one in a nested loop join? For example if the query is from a inner join b on..., which one, a, or b will be considered inner? I knew that it is b, but from the article at dbsophic, the first example under Small outer loop with a well indexed inner input seems to suggest the reverse.

like image 991
SexyBeast Avatar asked Aug 29 '12 12:08

SexyBeast


People also ask

Is join inner or left?

Different Types of SQL JOINs(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

Which rows are included in an inner join?

Inner Join Vs Other Joins Basically, these two clauses are the same. The INNER JOIN selects the common rows between two tables. Whereas the LEFT JOIN selects the common rows as well as all the remaining rows from the left table. The INNER JOIN selects the common rows between two tables.

What is inner loop join in SQL Server?

SQL Server Nested Loops Join ExplainedOne of the joining tables is designated as the outer table and another one as the inner table. For each row of the outer table, all the rows from the inner table are matched one by one if the row matches it is included in the result-set otherwise it is ignored.

What is an example of an inner join?

Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same for both the students and courses tables.


2 Answers

To be sure...

  • "INNER JOIN" is a the logical (relational) join operator
  • Inner and outer tables are concepts in the physical nested loop join operator

The choice of inner and outer tables for the physical operator is made by the optimiser and is unrelated to the logical operator.

Now, the nested loop psudeo code is this

for each row R1 in the outer table
    for each row R2 in the inner table
        if R1 joins with R2
            return (R1, R2)

So it doesn't make a difference in theory.

In practice, the optimiser will work out the best way around for inner and outer tables: which is what your article link should describe. A.k.a how to reduce the number of iterations

For completeness... INNER JOIN logical operator is commutative and associative
So A INNER JOIN B is the same as B INNER JOIN A.
There is no inner and outer table here

like image 178
gbn Avatar answered Sep 21 '22 01:09

gbn


Actually, both tables are inner as only rows are returned if there is a match in both tables.
When doing an outer join, you specify which table should be the outer one:

  • left outer join: The first table is the one for which all rows are returned and the second table is the one for which only the matching rows are returned.
  • right outer join: The second table is the one for which all rows are returned and the first table is the one for which only the matching rows are returned.
  • full outer join: All rows from both tables are returned.
like image 35
Daniel Hilgarth Avatar answered Sep 21 '22 01:09

Daniel Hilgarth