当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins

MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins

2018年09月05日  | 移动技术网IT编程  | 我要评论

mysql 查询优化之 block nested-loop 与 batched key access joins

在mysql中,可以使用批量密钥访问(bka)连接算法,该算法使用对连接表的索引访问和连接缓冲区。

bka算法支持:内连接,外连接和半连接操作,包括嵌套外连接。

bka的优点:更加高效的表扫描提高了连接性能。

此外,先前仅用于内连接的块嵌套循环(bnl)连接算法现已扩展,可用于外连接半连接操作,包括嵌套外连接

以下部分讨论了连接缓冲区管理,它是原始bnl算法扩展,扩展bnl算法和bka算法的基础。 有关半连接策略的信息,请参见

nested loop join算法

将外层表的结果集作为循环的基础数据,然后循环从该结果集每次一条获取数据作为下一个表的过滤条件去查询数据,然后合并结果。如果有多个表join,那么应该将前面的表的结果集作为循环数据,取结果集中的每一行再到下一个表中继续进行循环匹配,获取结果集并返回给客户端。

伪代码如下

for each row in t1 matching range {
  for each row in t2 matching reference key {
     for each row in t3 {
      if row satisfies join conditions,
      send to client
    }
  }
 }

 

普通的nested-loop join算法一次只能将一行数据传入内存循环,所以外层循环结果集有多少行,那么内存循环就要执行多少次。

block nested-loop算法

mysql bnl算法原本只支持内连接,现在已支持外连接半连接操作,包括嵌套外连接

bnl算法原理:将外层循环的行/结果集存入join buffer,内存循环的每一行数据与整个buffer中的记录做比较,可以减少内层循环的扫描次数

举个简单的例子:外层循环结果集有1000行数据,使用nlj算法需要扫描内层表1000次,但如果使用bnl算法,则先取出外层表结果集的100行存放到join buffer, 然后用内层表的每一行数据去和这100行结果集做比较,可以一次性与100行数据进行比较,这样内层表其实只需要循环1000/100=10次,减少了9/10。

伪代码如下

for each row in t1 matching range {
   for each row in t2 matching reference key {
    store used columns from t1, t2 in join buffer
    if buffer is full {
      for each row in t3 {
         for each t1, t2 combination in join buffer {
          if row satisfies join conditions,
          send to client
        }
        }
       empty buffer
     }
   }
 }

 if buffer is not empty {
    for each row in t3 {
     for each t1, t2 combination in join buffer {
       if row satisfies join conditions,
       send to client
      }
   }
 }

 

如果t1, t2参与join的列长度只和为s, c为二者组合数, 那么t3表被扫描的次数为

(s * c)/join_buffer_size + 1

 

扫描t3的次数随着join_buffer_size的增大而减少, 直到join buffer能够容纳所有的t1, t2组合, 再增大join buffer size, query 的速度就不会再变快了。

 

optimizer_switch系统变量的block_nested_loop标志控制优化器是否使用块嵌套循环算法。

默认情况下,block_nested_loop已启用。

在explain输出中,当extra值包含using join buffer(block nested loop)type值为all,index或range,表示使用bnl。

示例

mysql> explain select  a.gender, b.dept_no from employees a, dept_emp b where a.birth_date = b.from_date;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | extra                                              |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
|  1 | simple      | a     | null       | all  | null          | null | null    | null | 298936 |   100.00 | null                                               |
|  1 | simple      | b     | null       | all  | null          | null | null    | null | 331143 |    10.00 | using where; using join buffer (block nested loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

 

batched key access 算法

对于多表join语句,当mysql使用索引访问第二个join表的时候,使用一个join buffer来收集第一个操作对象生成的相关列值。bka构建好key后,批量传给引擎层做索引查找。key是通过mrr接口提交给引擎的,这样,mrr使得查询更有效率。

如果外部表扫描的是主键,那么表中的记录访问都是比较有序的,但是如果联接的列是非主键索引,那么对于表中记录的访问可能就是非常离散的。因此对于非主键索引的联接,batched key access join算法将能极大提高sql的执行效率。bka算法支持内连接,外连接和半连接操作,包括嵌套外连接。

batched key access join算法的工作步骤如下:

  • 1) 将外部表中相关的列放入join buffer中。

  • 2) 批量的将key(索引键值)发送到multi-range read(mrr)接口。

  • 3) multi-range read(mrr)通过收到的key,根据其对应的rowid进行排序,然后再进行数据的读取操作。

  • 4) 返回结果集给客户端。

batched key access join算法的本质上来说还是simple nested-loops join算法,其发生的条件为内部表上有索引,并且该索引为非主键,并且联接需要访问内部表主键上的索引。这时batched key access join算法会调用multi-range read(mrr)接口,批量的进行索引键的匹配和主键索引上获取数据的操作,以此来提高联接的执行效率,因为读取数据是以顺序磁盘io而不是随机磁盘io进行的。

使用bka时,join_buffer_size的值定义了对存储引擎的每个请求中批量密钥的大小。缓冲区越大,对连接操作的右侧表的顺序访问就越多,这可以显着提高性能。

要使用bka,必须将optimizer_switch系统变量的batched_key_access标志设置为on。 bka使用mrr,因此mrr标志也必须打开。目前,mrr的成本估算过于悲观。因此,mrr_cost_based也必须关闭才能使用bka。

以下设置启用bka:

mysql> set optimizer_switch='mrr=on,mrr_cost_based=off,batched_key_access=on';

 

在explain输出中,当extra值包含using join buffer(batched key access)且类型值为refeq_ref时,表示使用bka。

示例:

mysql> show index from employees;
+-----------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table     | non_unique | key_name       | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment |
+-----------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| employees |          0 | primary        |            1 | emp_no      | a         |      298936 |     null | null   |      | btree      |         |               |
| employees |          1 | idx_name       |            1 | last_name   | a         |        1679 |     null | null   |      | btree      |         |               |
| employees |          1 | idx_name       |            2 | first_name  | a         |      277495 |     null | null   |      | btree      |         |               |
| employees |          1 | idx_birth_date |            1 | birth_date  | a         |        4758 |     null | null   |      | btree      |         |               |
+-----------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)


mysql> explain select a.gender, b.dept_no from employees a, dept_emp b where a.birth_date = b.from_date;
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+-------+
| id | select_type | table | partitions | type | possible_keys  | key            | key_len | ref                   | rows   | filtered | extra |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+-------+
|  1 | simple      | b     | null       | all  | null           | null           | null    | null                  | 331143 |   100.00 | null  |
|  1 | simple      | a     | null       | ref  | idx_birth_date | idx_birth_date | 3       | employees.b.from_date |     62 |   100.00 | null  |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+-------+

#使用hint,强制走bka

mysql> explain select /*+ bka(a)*/ a.gender, b.dept_no from employees a, dept_emp b where a.birth_date = b.from_date;
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
| id | select_type | table | partitions | type | possible_keys  | key            | key_len | ref                   | rows   | filtered | extra                                  |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
|  1 | simple      | b     | null       | all  | null           | null           | null    | null                  | 331143 |   100.00 | null                                   |
|  1 | simple      | a     | null       | ref  | idx_birth_date | idx_birth_date | 3       | employees.b.from_date |     62 |   100.00 | using join buffer (batched key access) |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
2 rows in set, 1 warning (0.00 sec)

 

bnl和bka算法的优化器hint

除了使用optimizer_switch系统变量来控制优化程序在会话范围内使用bnl和bka算法之外,mysql还支持优化程序提示,以便在每个语句的基础上影响优化程序。 请参见“优化程序hint”

要使用bnl或bka提示为外部联接的任何内部表启用联接缓冲,必须为外部联接的所有内部表启用联接缓冲。

使用qb_name

select /*+ qb_name(qb1) mrr(@qb1 t1) bka(@qb2) no_mrr(@qb3t1 idx1, id2) */ ...
  from (select /*+ qb_name(qb2) */ ...
  from (select /*+ qb_name(qb3) */ ... from ...)) ...

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网