MySql Join connection three algorithms

MySql Join connection three algorithms

Foreword:

MySQL only supports one JOIN algorithm, Nested-Loop Join (Nested-Loop Join), but MySQL's Nested-Loop Join (Nested-Loop Join) also has many variants, which can help MySQL perform JOIN operations more efficiently.

1. Simple Nested-Loop Join

Insert picture description here

This algorithm is relatively simple. Take the associated condition A1 from the driving table A to match all the columns of the driven table B, and get the result record. Continue to A2, A3... until all the associated conditions in the drive table A are matched. Then merge all records. This algorithm has to make An access to the driven table B, and each access to the driven table B is a full table scan, which has very poor performance.

2. Index Nested-Loop Join

Insert picture description here

Take the associated condition A1 from the driving table A to match the driven table B. The driven table B is based on the index query very fast (without a full table scan), and the result record is obtained. Continue to A2, A3... until all the associated conditions in the drive table A are matched. This algorithm needs to make An access to the driven table B, and the driven table B is very fast based on the index query each time, and the performance depends entirely on the number of associated conditions of the driven table A.

3. Block Nested-Loop Join

Insert picture description here

​ In the case of an index, MySQL will try to use the Index Nested-Loop Join algorithm. In some cases, the Join column may not have an index. At this time, the choice of MySQL will definitely not be the first Simple Nested- Loop Join algorithm, but the Block Nested-Loop Join algorithm will be used first.

​ Block Nested-Loop Join compares Simple Nested-Loop Join with an additional intermediate processing process, that is, join buffer. Use join buffer to buffer all query JOIN related columns of the driving table into JOIN BUFFER, and then batch and non-driven table For comparison, if this is also achieved, multiple comparisons can be combined into one, which reduces the frequency of access to non-driven tables.

​ In MySQL, we can set the value of the join buffer through the parameter join_buffer_size, and then perform operations. By default, join_buffer_size=256K. When searching, MySQL will cache all required columns in the join buffer, including select columns, instead of just caching associated columns.

Guess you like

Origin blog.csdn.net/weixin_44981707/article/details/110576470