join algorithmMySQL only supports one join algorithm: Nested-Loop Join, but Nested-Loop Join has three variants:
The difference between driven and non-driven tablesThe driving table is the main table, and the non-driven table is the slave table. You can see it by looking at the following SQL. A is the driving table and B is the non-driven table. select * from A left join B When A joins B, must we check table A first and then table B? The answer is not necessarily, because MySQL has an optimizer inside, which will make some optimizations based on your query statements. The optimizer also determines which table to check first, but it is certain that the table that is checked first is the driving table, and vice versa. As for the question of which table to check first, we can get the result by looking at the execution plan; just add the explain keyword in front; explain select * from A join B; 1. Simple Nested-Loop Join, simple nesting-no indexA left join B: Each time, the whole table is matched. Each row of data in table A is matched once in table B. That is to say, if there are 10 records in table A and 1000 records in table B, the number of scans during query is 10*1000, which means that the query needs to scan 10000 times to get the data. 2. Index Nested-Loop Join-Indexed Case select * from A join B on A.id=B.id where A.id = 1: When querying, the driver table A will search according to the index of the associated field. When a matching value is found on the index, the query will be returned to the table. In other words, the query will be returned to the table only after the index is matched. 3. Block Nested-Loop Join, join bufferIf there is an index, Index Nested-Loop Join will be used to join the tables. If there is no index on the join column, Block Nested-Loop Join will be used. Join buffer. There is a buffer between the driver table and the non-driver table. When querying, the data of the driver table is first cached in the buffer, and then matched with the non-driver table in batches. This is an optimization solution that combines multiple comparisons into one comparison. Note: not only the columns of the associated table are cached here, but also the columns after the select. Buffer sizeBy default, the capacity of the buffer join_biffer_size is 256k. If your data space is larger than 256k, the buffer cannot be used and the join will be converted to the simplest Simple Nested-Loop Join. However, we can manually adjust the buffer size to load large amounts of data. View the join_biffer_size sql: show variables like '%join_biffer_size%' How to choose the connection order for tables with large and small data volumesIt is best to connect the small table to the big table, which will reduce the number of scans; for example, if the big table has 1,000 data records and the small table has only 10 data records, then the best connection method is: small table joins the big table; why do you do this?
detail
This is the end of this article about the underlying principles of MySQL join. For more information about the underlying principles of MySQL join, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue implements small search function
>>: Determine the direction of mouse entry based on CSS
Use HSSFWorkbook in Apache.POI to export to Excel...
MySQL installation is divided into installation v...
1. Business Scenario I have been doing developmen...
Table of contents Preface Asynchronous loading Pa...
When using docker-compose for deployment, the out...
Table of contents 1. Demand 2. Effect 3. All code...
The following two errors were encountered when co...
Foregoing: This document is based on the assumpti...
Nginx log description Through access logs, you ca...
background Recently, a leader wanted us to build ...
Moreover, an article website built with a blog pro...
This article mainly introduces the ::master pseud...
Docker Overview Docker is an open source software...
This article uses an example to describe how to a...
When the Docker container exits, the file system ...