mysql's small table drives the big table principle

What is a driving table

When performing multi-table join query, the definition of the driving table is:

  • If the connection condition is specified, the table with fewer rows that meet the query condition is the driving table
  • If the connection condition is not specified, the table with fewer rows is the driving table

Can also EXPLAINview SQLmay determine the execution plan statement of who is driving the table, EXPLAINthe statement analysis out of the first row of the table that is the driving table

mysql Why use a small table to drive the principle of a large table

mysqlThe algorithm of table association Nest Loop Joinis to use the result set of the driving table as the loop basic data, and then use the data in the result set as the filter condition to query the data in the next table, and then merge the results

Example: Usertable there are 10000pieces of data, Classtable there are 20pieces of data

-- 驱动表为 Class 表
select * from user as u left join class as c u.userid=c.userid

We need to use this Usertable circulation 10000can check out times, and if the Classtable driver Useronly needs to loop table 20times will be able to check out

The so-called small table drives a large table: that is, a small result set is used to drive a large result set

JOIN Query how to determine the driving table and the driven table

  • LEFT JOIN: The left table (master table) is the driven table, the right table (slave table) is the driven table
  • RIGHT JOIN:The right table (master table) is the driven table, the left table (slave table) is the driven table
  • INNER JOIN: mysqlThe table with a relatively small amount of data will be selected as the driving table, and the large table as the driven table

INAnd EXISTSusing the difference

  • If the amount of data query two tables rather then using inand existsnot very different
  • If one of the two tables has a smaller amount of data and the other has a larger amount of data, the larger amount of existstable data in the subquery is used , and the smaller amount of table data in the subquery is usedin
select * from A where id in(select id from B)

When the Btable data is set to be smaller than the Adata table is set, with INbetter EXISTS( SQLthe efficiency): As Bthe amount of data table < Aamount data table for this section SQL, the Btable is a drive table, using mysqla small table-driven large The principle of the table. So its execution order is: first execute the subquery, then execute the main query, use the results of the subquery to match the main query according to the conditions

select * from A where exists (select 1 from B where B.id = A.id)

When the Atable is smaller than the data set Bwhen the data set table, with EXISTSbetter IN( SQLthe efficiency of): it is the first execution order main query is executed, the data in the main query conditions do verification subquery

Guess you like

Origin blog.csdn.net/weixin_38192427/article/details/115189355