SQL left join underlying principle of

Introduction

left joinThe effect is achieved to retain all the information left table, the stitching on the right table left the table, if the fight was not on NULL.

In addition to left joinoutside, as well as inner join, outer join, right joinand so on, the article does not describe the effect of other specific connections, mainly on jointhe underlying principle is how to achieve? What is the specific effect rendered?

Only you understand the underlying principles, in order better to write the superior performance of SQL scripts, SQL execution speed increase.

joinThere are three main ways, in particular:

  • Nested Loop - nested loops, broken down into the following three kinds of connections:
    • Simple Nested-Loop Join
    • Index Nested-Loop Join
    • Block Nested-Loop Join
  • Hash Join
  • Merge Join

Other concepts:

Drive table (also called the look) and driven table (also called non-driving table, also called the match table, also called in the table), simply, the drive table is the primary table, left jointhe table is left drive table, right joinin the right table is the driving table.

A drive table, it can only other non-driven table, in the joinprocess, in fact, from the drive table which in turn (note that there appreciated sequentially) for each value taken, and then to match the non-drive table inside.

Simple Nested-Loop Join

Simple Nested-Loop JoinThese three methods which are the easiest and best understood, and most everyone in line with a connection method of cognition.

There are two tables table Aand table B, let us table A left join table Bobserve how the match.



From the driving table table Aare taken sequentially for each value, then go to the non-driving table table Bis performed down sequentially from the match , to return a value.

In this way matches in total need to perform 10×10=100queries.

Index Nested-Loop Join

Using the index matching Indexrequired on a non-driven table with an index, the index can be reduced since the number of matches had to improve search efficiency.

Commonly used index database B+树storage structure, the efficiency can be improved.

If the index is a primary key, then, will be more efficient because the primary key must be unique. So if the table is being driven by the primary key to connect, or many-to-one situation will arise, and will not be the case and many-to-many.

Block Nested-Loop Join

Ideally, the index match is the most efficient kind of way, but in the real work, not all of the columns are index columns, this time you need to use Block Nested-Loop Join a method, this method first a method relatively similar, the only difference is that will drive the table relates to the left join all of the columns (columns than are used, as well as on the column select section) taken out into a first cache area, and then go and non-driven table matching, matching the number of times this method and the first method is required compared to the same, the difference lies in the number of different columns of a table driven, that is, the number of different amounts of data.

Therefore, although the number of matches has not decreased, but still have the overall query performance improvement.

Reference links: SQL underlying principle

Guess you like

Origin www.cnblogs.com/hider/p/11616518.html