用 EXPLAIN 查看子查询的执行计划

    本文档所使用的示例表数据如下:

    以下示例中,IN 子查询会从表 t2 中搜索一列 ID。为保证语义正确性,TiDB 需要保证 t1_id 列的值具有唯一性。使用 EXPLAIN 可查看到该查询的执行计划去掉重复项并执行 Inner Join 内连接操作:

    1. EXPLAIN SELECT * FROM t1 WHERE id IN (SELECT t1_id FROM t2);
    1. +----------------------------------+----------+-----------+------------------------------+---------------------------------------------------------------------------------------------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +----------------------------------+----------+-----------+------------------------------+---------------------------------------------------------------------------------------------------------------------------+
    4. | IndexJoin_14 | 5.00 | root | | inner join, inner:IndexLookUp_13, outer key:test.t2.t1_id, inner key:test.t1.id, equal cond:eq(test.t2.t1_id, test.t1.id) |
    5. | ├─StreamAgg_49(Build) | 5.00 | root | | group by:test.t2.t1_id, funcs:firstrow(test.t2.t1_id)->test.t2.t1_id |
    6. | └─IndexReader_50 | 5.00 | root | | index:StreamAgg_39 |
    7. | └─StreamAgg_39 | 5.00 | cop[tikv] | | group by:test.t2.t1_id, |
    8. | └─IndexFullScan_31 | 50000.00 | cop[tikv] | table:t2, index:t1_id(t1_id) | keep order:true |
    9. | └─IndexLookUp_13(Probe) | 1.00 | root | | |
    10. | ├─IndexRangeScan_11(Build) | 1.00 | cop[tikv] | table:t1, index:PRIMARY(id) | range: decided by [eq(test.t1.id, test.t2.t1_id)], keep order:false |
    11. +----------------------------------+----------+-----------+------------------------------+---------------------------------------------------------------------------------------------------------------------------+
    12. 8 rows in set (0.00 sec)

    由上述查询结果可知,TiDB 通过索引连接操作 | IndexJoin_14 将子查询做了连接转化。该执行计划首先在 TiKV 侧通过索引扫描算子 └─IndexFullScan_31 读取 t2.t1_id 列的值,然后由 └─StreamAgg_39 算子的部分任务在 TiKV 中对 t1_id 值进行去重,然后采用 ├─StreamAgg_49(Build) 算子的部分任务在 TiDB 中对 值再次进行去重,去重操作由聚合函数 firstrow(test.t2.t1_id) 执行;之后将操作结果与 t1 表的主键相连接,连接条件是 eq(test.t1.id, test.t2.t1_id)

    1. +----------------------------------+---------+-----------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +----------------------------------+---------+-----------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------+
    4. | IndexJoin_17 | 1978.13 | root | | inner join, inner:IndexLookUp_16, outer key:test.t3.t1_id, inner key:test.t1.id, equal cond:eq(test.t3.t1_id, test.t1.id) |
    5. | ├─TableReader_44(Build) | 1978.00 | root | | data:TableFullScan_43 |
    6. | └─TableFullScan_43 | 1978.00 | cop[tikv] | table:t3 | keep order:false |
    7. | └─IndexLookUp_16(Probe) | 1.00 | root | | |
    8. | ├─IndexRangeScan_14(Build) | 1.00 | cop[tikv] | table:t1, index:PRIMARY(id) | range: decided by [eq(test.t1.id, test.t3.t1_id)], keep order:false |
    9. | └─TableRowIDScan_15(Probe) | 1.00 | cop[tikv] | table:t1 | keep order:false |
    10. +----------------------------------+---------+-----------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------+
    11. 6 rows in set (0.01 sec)

    从语义上看,因为约束保证了 t3.t1_id 列值的唯一性,TiDB 可以直接执行 INNER JOIN 查询。

    在前两个示例中,通过 StreamAgg 聚合操作或通过 UNIQUE 约束保证子查询数据的唯一性之后,TiDB 才能够执行 Inner Join 操作。这两种连接均使用了 Index Join

    下面的例子中,TiDB 优化器则选择了一种不同的执行计划:

    1. EXPLAIN SELECT * FROM t1 WHERE id IN (SELECT t1_id FROM t2 WHERE t1_id != t1.int_col);

    可以将原语句视为关联子查询,因为它引入了子查询外的 t1.int_col 列。然而,EXPLAIN 语句的返回结果显示的是后的执行计划。条件 t1_id != t1.int_col 会被重写为 t1.id != t1.int_col。TiDB 可以从表 t1 中读取数据并且在 └─Selection_21 中执行此操作,因此这种去关联和重写操作会极大提高执行效率。

    在以下示例中,除非子查询中存在 t3.t1_id,否则该查询将(从语义上)返回表 t3 中的所有行:

    1. EXPLAIN SELECT * FROM t3 WHERE t1_id NOT IN (SELECT id FROM t1 WHERE int_col < 100);
    1. +----------------------------------+---------+-----------+-----------------------------+-------------------------------------------------------------------------------------------------------------------------------+
    2. +----------------------------------+---------+-----------+-----------------------------+-------------------------------------------------------------------------------------------------------------------------------+
    3. | IndexJoin_14 | 1582.40 | root | | anti semi join, inner:IndexLookUp_13, outer key:test.t3.t1_id, inner key:test.t1.id, equal cond:eq(test.t3.t1_id, test.t1.id) |
    4. | ├─TableReader_35(Build) | 1978.00 | root | | data:TableFullScan_34 |
    5. | └─TableFullScan_34 | 1978.00 | cop[tikv] | table:t3 | keep order:false |
    6. | └─IndexLookUp_13(Probe) | 1.00 | root | | |
    7. | ├─IndexRangeScan_10(Build) | 1.00 | cop[tikv] | table:t1, index:PRIMARY(id) | range: decided by [eq(test.t1.id, test.t3.t1_id)], keep order:false |
    8. | └─Selection_12(Probe) | 1.00 | cop[tikv] | | lt(test.t1.int_col, 100) |
    9. | └─TableRowIDScan_11 | 1.00 | cop[tikv] | table:t1 | keep order:false |
    10. +----------------------------------+---------+-----------+-----------------------------+-------------------------------------------------------------------------------------------------------------------------------+
    11. 7 rows in set (0.00 sec)

    上述查询首先读取了表 t3,然后根据主键开始探测 (probe) 表 t1。连接类型是 anti semi join,即反半连接:之所以使用 anti,是因为上述示例有不存在匹配值(即 NOT IN)的情况;使用 则是因为仅需要匹配第一行后就可以停止查询。