数据库实践丨MySQL多表join分析

数据库 MySQL
在数据库查询中,往往会需要查询多个表的数据,比如查询会员信息同时查询关于这个会员的订单信息,如果分语句查询的话,效率会很低,就需要用到join关键字来连表查询了。

Join并行

Join并行1. 多表join介绍2. 多表Join的方式不使用Join buffer使用Join buffer3. Join执行流程(老执行器)

1. 多表join介绍

JOIN子句用于根据两个或多个表之间的相关列来组合它们。 例如:

Orders:

Customers:

 

  1. SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate  
  2. FROM Orders  
  3. INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID; 

2. 多表Join的方式

Hash join使用新执行器实现,在这里不做讨论

MySQL支持的都是Nested-Loop Join,以及它的变种。

不使用Join buffer

a) Simple Nested-Loop

对r表的每一行,完整扫描s表,根据r[i]-s[i]组成的行去判断是否满足条件,并返回满足条件的结果给客户端。

 

  1. mysql> show create table t1;  
  2. +-------+----------------------------------------------------------------------------------------------------------------+  
  3. | Table | Create Table                                                                                                   |  
  4. +-------+----------------------------------------------------------------------------------------------------------------+  
  5. | t1    | CREATE TABLE `t1` (  
  6.  `id` int(11) NOT NULL  
  7. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  8. +-------+----------------------------------------------------------------------------------------------------------------+  
  9. 1 row in set (0.00 sec)  
  10. mysql> show create table t3;  
  11. +-------+--------------------------------------------------------------------------------------------------------------------+  
  12. | Table | Create Table                                                                                                       |  
  13. +-------+--------------------------------------------------------------------------------------------------------------------+  
  14. | t3    | CREATE TABLE `t3` (  
  15.  `id` int(11) DEFAULT NULL  
  16. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  17. +-------+--------------------------------------------------------------------------------------------------------------------+  
  18. 1 row in set (0.00 sec)  
  19. mysql> explain select /*+ NO_BNL() */ * from t1, t3 where t1.id = t3.id;  
  20. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  21. | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |  
  22. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  23. |  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL        |  
  24. |  1 | SIMPLE      | t3    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |    50.00 | Using where |  
  25. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  26. 2 rows in set, 1 warning (0.00 sec) 

b) Index Nested-Loop

对r表的每一行,先根据连接条件去查询s表索引,然后回表查到匹配的数据,并返回满足条件的结果给客户端。

 

  1. mysql> show create table t2;  
  2. +-------+---------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Table | Create Table                                                                                                                          |  
  4. +-------+---------------------------------------------------------------------------------------------------------------------------------------+  
  5. | t2    | CREATE TABLE `t2` (  
  6.  `id` int(11) NOT NULL,  
  7.  KEY `index1` (`id`)  
  8. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  9. +-------+---------------------------------------------------------------------------------------------------------------------------------------+  
  10. 1 row in set (0.00 sec)  
  11. mysql> explain select * from t1, t2 where t1.id = t2.id;  
  12. +----+-------------+-------+------------+------+---------------+--------+---------+------------+------+----------+-------------+  
  13. | id | select_type | table | partitions | type | possible_keys | key    | key_len | ref        | rows | filtered | Extra       |  
  14. +----+-------------+-------+------------+------+---------------+--------+---------+------------+------+----------+-------------+  
  15. |  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL   | NULL    | NULL       |    2 |   100.00 | NULL        |  
  16. |  1 | SIMPLE      | t2    | NULL       | ref  | index1        | index1 | 4       | test.t1.id |    1 |   100.00 | Using index |  
  17. +----+-------------+-------+------------+------+---------------+--------+---------+------------+------+----------+-------------+  
  18. 2 rows in set, 1 warning (0.00 sec)  

使用Join buffer

a) Block Nested Loop

从r表读取一部分数据到join cache中,当r表数据读完或者join cache满后,做join操作。 

  1. JOIN_CACHE_BNL::join_matching_records(){  
  2.  do {  
  3.    //读取s表的每一行 
  4.     qep_tab->table()->file->position(qep_tab->table()->record[0]);  
  5.    //针对s的每一行,遍历join buffer  
  6.    for(each record in join buffer) {  
  7.      get_record();  
  8.      rc = generate_full_extensions(get_curr_rec());  
  9.      //如果不符合条件,直接返回  
  10.      if (rc != NESTED_LOOP_OK) return rc;  
  11.    }  
  12.  } while(!(error = iterator->Read()))  
  13.  
  1. mysql> explain select  * from t1, t3 where t1.id = t3.id;  
  2. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+  
  3. | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                              |  
  4. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+  
  5. |  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL                                               |  
  6. |  1 | SIMPLE      | t3    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |    50.00 | Using where; Using join buffer (Block Nested Loop) |  
  7. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+  
  8. 2 rows in set, 1 warning (0.00 sec) 

 b) Batched Key Access

从r表读取一部分数据到join cache中,s表中记录r表被连接的列的值作为索引,查询所有符合条件的索引,然后将这些符合条件的索引排序,然后统一回表查询记录。

其中,对于每一个cached record,都会有一个key,通过这个key去s表扫描所需的数据。 

  1. dsmrr_fill_buffer(){  
  2.  while((rowids_buf_cur < rowids_buf_end) &&  
  3.        !(res = h2->handler::multi_range_read_next(&range_info))){  
  4.    //下压的index条件  
  5.    if (h2->mrr_funcs.skip_index_tuple &&  
  6.        h2->mrr_funcs.skip_index_tuple(h2->mrr_iter, curr_range->ptr))  
  7.      continue;  
  8.    memcpy(rowids_buf_cur, h2->ref, h2->ref_length);  
  9.  }  
  10.  varlen_sort(  
  11.      rowids_buf, rowids_buf_cur, elem_size,  
  12.      [this](const uchar *a, const uchar *b) { return h->cmp_ref(a, b) < 0; });  
  13.  
  14. dsmrr_next(){  
  15.  do{  
  16.    if (rowids_buf_cur == rowids_buf_last) {  
  17.      dsmrr_fill_buffer();  
  18.    }  
  19.    // first match  
  20.    if (h2->mrr_funcs.skip_record &&  
  21.        h2->mrr_funcs.skip_record(h2->mrr_iter, (char *)cur_range_info, rowid))  
  22.      continue;  
  23.    res = h->ha_rnd_pos(table->record[0], rowid);  
  24.    break;  
  25.  } while(true);  
  26.  
  27. JOIN_CACHE_BKA::join_matching_records(){  
  28.  while (!(error = file->ha_multi_range_read_next((char **)&rec_ptr))) {  
  29.    get_record_by_pos(rec_ptr);  
  30.    rc = generate_full_extensions(rec_ptr);  
  31.      if (rc != NESTED_LOOP_OK) return rc;  
  32.  }  
  33.  
  1. mysql> show create table t1;  
  2. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Table | Create Table                                                                                                                                    |  
  4. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+  
  5. | t1    | CREATE TABLE `t1` (  
  6.  `f1` int(11) DEFAULT NULL, 
  7.  `f2` int(11) DEFAULT NULL  
  8. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  9. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------+  
  10. 1 row in set (0.00 sec)  
  11. mysql> show create table t2;  
  12. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  13. | Table | Create Table                                                                                                                                                                                | 
  14. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  15. | t2    | CREATE TABLE `t2` (  
  16.  `f1` int(11) NOT NULL,  
  17.  `f2` int(11) NOT NULL,  
  18.  `f3` char(200) DEFAULT NULL,  
  19.  KEY `f1` (`f1`,`f2`)  
  20. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  21. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  22. 1 row in set (0.00 sec)  
  23. mysql> explain SELECT /*+ BKA() */ t2.f1, t2.f2, t2.f3 FROM t1,t2 WHERE t1.f1=t2.f1 AND t2.f2 BETWEEN t1.f1 and t1.f2 and t2.f2 + 1 >= t1.f1 + 1;  
  24. +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+---------------------------------------------------------------+ 
  25. | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref         | rows | filtered | Extra                                                      | 
  26. +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+---------------------------------------------------------------+ 
  27. |  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL        |    3 |   100.00 | Using where                                                   | 
  28. |  1 | SIMPLE      | t2    | NULL       | ref  | f1            | f1   | 4       | test1.t1.f1 |    7 |    11.11 | Using index condition; Using join buffer (Batched Key Access) | 
  29. +----+-------------+-------+------------+------+---------------+------+---------+-------------+------+----------+---------------------------------------------------------------+ 
  30. 2 rows in set, 1 warning (0.00 sec) 

c) Batched Key Access(unique)

与Batched Key Access不同的是,r中的列是s的唯一索引,在r记录写入join cache的时候,会记录一个key的hash table,仅针对不同的key去s表中查询。(疑问,为什么只有unique的时候才能用这种方式?不是unique的话,s表中可能会扫描出多条数据,也可以用这种方式去处理,减少s表的重复扫描)。 

  1. JOIN_CACHE_BKA_UNIQUE::join_matching_records(){  
  2.  while (!(error = file->ha_multi_range_read_next((char **)&key_chain_ptr))) {  
  3.    do(each record in chain){  
  4.      get_record_by_pos(rec_ptr);  
  5.      rc = generate_full_extensions(rec_ptr);  
  6.        if (rc != NESTED_LOOP_OK) return rc;  
  7.      }  
  8.  }  
  9.  
  1. mysql> show create table city;  
  2. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  3. | Table | Create Table                                                                                                                                                                                                                                                                                                                                       | 
  4. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  5. | city  | CREATE TABLE `city` (  
  6.  `ID` int(11) NOT NULL AUTO_INCREMENT,  
  7.  `Name` char(35) NOT NULL DEFAULT '',  
  8.  `Country` char(3) NOT NULL DEFAULT '',  
  9.  `Population` int(11) NOT NULL DEFAULT '0',  
  10.  PRIMARY KEY (`ID`),  
  11.  KEY `Population` (`Population`),  
  12.  KEY `Country` (`Country`)  
  13. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  14. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  15. 1 row in set (0.00 sec)  
  16. mysql> show create table country;  
  17. +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  18. | Table   | Create Table                                                                                                                                                                                                                                                                                                                                                   | 
  19. +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  20. | country | CREATE TABLE `country` (  
  21.  `Code` char(3) NOT NULL DEFAULT '',  
  22.  `Name` char(52) NOT NULL DEFAULT '',  
  23.  `SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',  
  24.  `Population` int(11) NOT NULL DEFAULT '0',  
  25.  `Capital` int(11) DEFAULT NULL,  
  26.  PRIMARY KEY (`Code`),  
  27.  UNIQUE KEY `Name` (`Name`)  
  28. ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |  
  29. +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  30. 1 row in set (0.01 sec)  
  31. mysql> EXPLAIN SELECT city.Name, country.Name FROM city,country WHERE city.country=country.Code AND  country.Name LIKE 'L%' AND city.Population > 100000; 
  32. +----+-------------+---------+------------+-------+--------------------+---------+---------+--------------------+------+----------+--------------------------------------------------------------+ 
  33. | id | select_type | table   | partitions | type  | possible_keys      | key     | key_len | ref                | rows | filtered | Extra                                                        | 
  34. +----+-------------+---------+------------+-------+--------------------+---------+---------+--------------------+------+----------+--------------------------------------------------------------+ 
  35. |  1 | SIMPLE      | country | NULL       | index | PRIMARY,Name       | Name    | 208     | NULL               |    1 |   100.00 | Using where; Using index                                     | 
  36. |  1 | SIMPLE      | city    | NULL       | ref   | Population,Country | Country | 12      | test1.country.Code |    1 |   100.00 | Using where; Using join buffer (Batched Key Access (unique)) | 
  37. +----+-------------+---------+------------+-------+--------------------+---------+---------+--------------------+------+----------+--------------------------------------------------------------+ 
  38. 2 rows in set, 1 warning (0.01 sec) 

 3. Join执行流程(老执行器)

 

  1. sub_select <-------------------------------------------- 
  2.  | -> iterator::read() // 读一行数据                    |  
  3.  | -> evaluate_join_record()  //检查这行数据是否符合条件 |  
  4.  | -> next_select() ---+                               |  
  5.                        |                               |  
  6. sub_select_op  <--------+                               |  
  7.  | -> op->put_record() // 前表数据写入join cache        |  
  8.    | -> put_record_in_cache()                          |  
  9.    | -> join->record()                                 |  
  10.      | -> join_matching_records()                      |  
  11.        | -> (qep_tab->next_select)(join, qep_tab + 1, 0) // 继续调用next_select  
  12.    | -> end_send()  

 

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2022-09-05 10:06:21

MySQL外循环内循环

2018-07-30 15:00:05

数据库MySQLJOIN

2017-07-18 17:07:40

数据库 MyCATJoin

2010-05-21 14:01:23

MySQL数据库

2010-06-07 16:22:55

MySQL数据库

2010-06-02 18:07:44

MySQL数据库

2011-07-06 10:49:50

MySQL优化

2011-07-06 14:12:20

MySQLPercona

2018-06-26 15:58:06

数据库MySQL索引优化

2021-04-09 08:21:25

数据库索引数据

2023-09-12 09:45:54

Java数据库

2024-05-28 00:00:30

Golang数据库

2022-02-10 10:51:35

数据库

2017-06-22 16:00:07

数据库NoSQL迁移实践

2013-10-08 09:54:41

数据库安全数据库管理

2017-03-15 15:14:03

MySQL数据库高可用性

2011-08-23 18:19:19

Oracle行转列Join用法

2017-11-22 09:20:41

数据库在线数据迁移Subscriptio

2010-11-30 11:26:49

点赞
收藏

51CTO技术栈公众号