Skip to content

Commit 6c7444e

Browse files
authored
update doc for IndexMerge GA (pingcap#8082)
1 parent b33f914 commit 6c7444e

11 files changed

+28
-26
lines changed

explain-aggregation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,4 @@ Query OK, 0 rows affected (0.28 sec)
186186
+ [子查询的执行计划](/explain-subqueries.md)
187187
+ [视图查询的执行计划](/explain-views.md)
188188
+ [分区查询的执行计划](/explain-partitions.md)
189-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
189+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-index-merge.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
---
2-
title: 用 EXPLAIN 查看开启 IndexMerge 的 SQL 执行计划
2+
title: 用 EXPLAIN 查看索引合并的 SQL 执行计划
33
summary: 了解 TiDB 中 EXPLAIN 语句返回的执行计划信息。
44
---
55

6-
# 用 EXPLAIN 查看开启 IndexMerge 的 SQL 执行计划
6+
# 用 EXPLAIN 查看索引合并的 SQL 执行计划
77

8-
`IndexMerge`TiDB v4.0 中引入的一种对表的新访问方式。在这种访问方式下,TiDB 优化器可以选择对一张表使用多个索引,并将每个索引的返回结果进行合并。在某些场景下,这种访问方式能够减少大量不必要的数据扫描,提升查询的执行效率。
8+
索引合并是从 TiDB v4.0 起引入的一种新的表访问方式。在这种访问方式下,TiDB 优化器可以选择对一张表使用多个索引,并将每个索引的返回结果进行合并。在某些场景下,这种访问方式能够减少大量不必要的数据扫描,提升查询的执行效率。
99

1010
```sql
11-
EXPLAIN SELECT * from t where a = 1 or b = 1;
11+
EXPLAIN SELECT * FROM t WHERE a = 1 OR b = 1;
1212
+-------------------------+----------+-----------+---------------+--------------------------------------+
1313
| id | estRows | task | access object | operator info |
1414
+-------------------------+----------+-----------+---------------+--------------------------------------+
1515
| TableReader_7 | 8000.00 | root | | data:Selection_6 |
1616
| └─Selection_6 | 8000.00 | cop[tikv] | | or(eq(test.t.a, 1), eq(test.t.b, 1)) |
1717
| └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t | keep order:false, stats:pseudo |
1818
+-------------------------+----------+-----------+---------------+--------------------------------------+
19-
set @@tidb_enable_index_merge = 1;
20-
explain select * from t use index(idx_a, idx_b) where a > 1 or b > 1;
19+
EXPLAIN SELECT /*+ USE_INDEX_MERGE(t) */ * FROM t WHERE a > 1 OR b > 1;
2120
+--------------------------------+---------+-----------+-------------------------+------------------------------------------------+
2221
| id | estRows | task | access object | operator info |
2322
+--------------------------------+---------+-----------+-------------------------+------------------------------------------------+
@@ -28,21 +27,18 @@ explain select * from t use index(idx_a, idx_b) where a > 1 or b > 1;
2827
+--------------------------------+---------+-----------+-------------------------+------------------------------------------------+
2928
```
3029

31-
例如,在上述示例中,过滤条件是使用 `OR` 条件连接的 `WHERE` 子句。在启用 `IndexMerge`,每个表只能使用一个索引,不能将 `a = 1` 下推到索引 `a`,也不能将 `b = 1` 下推到索引 `b`。当 `t` 中存在大量数据时,全表扫描的效率会很低。针对这类场景,TiDB 引入了对表的新访问方式 `IndexMerge`
30+
例如,在上述示例中,过滤条件是使用 `OR` 条件连接的 `WHERE` 子句。在启用索引合并前,每个表只能使用一个索引,不能将 `a = 1` 下推到索引 `a`,也不能将 `b = 1` 下推到索引 `b`。当 `t` 中存在大量数据时,全表扫描的效率会很低。针对这类场景,TiDB 引入了对表的新访问方式:索引合并
3231

33-
`IndexMerge` 访问方式下,优化器可以选择对一张表使用多个索引,并将每个索引的返回结果进行合并,生成以上示例中后一个 `IndexMerge` 的执行计划。此时的 `IndexMerge_16` 算子有三个子节点,其中 `IndexRangeScan_13``IndexRangeScan_14` 根据范围扫描得到符合条件的所有 `RowID`,再由 `TableRowIDScan_15` 算子根据这些 `RowID` 精确地读取所有满足条件的数据。
32+
在索引合并访问方式下,优化器可以选择对一张表使用多个索引,并将每个索引的返回结果进行合并,生成以上示例中后一个执行计划。此时的 `IndexMerge_16` 算子有三个子节点,其中 `IndexRangeScan_13``IndexRangeScan_14` 根据范围扫描得到符合条件的所有 `RowID`,再由 `TableRowIDScan_15` 算子根据这些 `RowID` 精确地读取所有满足条件的数据。
3433

3534
其中对于 `IndexRangeScan`/`TableRangeScan` 一类按范围进行的扫表操作,`EXPLAIN` 表中 `operator info` 列相比于其他扫表操作,多了被扫描数据的范围这一信息。比如上面的例子中,`IndexRangeScan_13` 算子中的 `range:(1,+inf]` 这一信息表示该算子扫描了从 1 到正无穷这个范围的数据。
3635

3736
> **注意:**
3837
>
39-
> 目前,TiDB 的 `IndexMerge` 特性在 TiDB 4.0.0-rc.1 版本中默认关闭。同时 4.0 版本中的 `IndexMerge` 目前支持的场景仅限于析取范式(`or` 连接的表达式),暂不支持合取范式(`and` 连接的表达式)。开启 `IndexMerge` 特性有以下方法:
40-
>
41-
> - 设置系统变量 `tidb_enable_index_merge=1`
42-
>
43-
> - 在查询中使用 SQL 优化器 Hint [`USE_INDEX_MERGE`](/optimizer-hints.md#use_index_merget1_name-idx1_name--idx2_name-)
44-
>
45-
> SQL Hint 的优先级高于系统变量。
38+
> - TiDB 的索引合并特性在 v5.4.0 及之后的版本默认开启,即 [`tidb_enable_index_merge`](/system-variables.md#tidb_enable_index_merge-从-v40-版本开始引入)`ON`
39+
> - 如果查询中使用了 SQL 优化器 Hint [`USE_INDEX_MERGE`](/optimizer-hints.md#use_index_merget1_name-idx1_name--idx2_name-) ,无论 `tidb_enable_index_merge` 开关是否开启,都会强制使用索引合并特性。当过滤条件中有无法下推的表达式时,必须使用 Hint [`USE_INDEX_MERGE`](/optimizer-hints.md#use_index_merget1_name-idx1_name--idx2_name-) 才能开启索引合并。
40+
> - 索引合并目前仅支持析取范式(`or` 连接的表达式),不支持合取范式(`and` 连接的表达式)。
41+
> - 索引合并目前无法在临时表上使用。
4642
4743
## 其他类型查询的执行计划
4844

explain-indexes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,4 @@ EXPLAIN SELECT AVG(intkey), ANY_VALUE(pad1) FROM t1;
357357
+ [聚合查询的执行计划](/explain-aggregation.md)
358358
+ [视图查询的执行计划](/explain-views.md)
359359
+ [分区查询的执行计划](/explain-partitions.md)
360-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
360+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-joins.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,4 @@ TiDB 会按照以下顺序执行 Merge Join 算子:
310310
+ [聚合查询的执行计划](/explain-aggregation.md)
311311
+ [视图查询的执行计划](/explain-views.md)
312312
+ [分区查询的执行计划](/explain-partitions.md)
313-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
313+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-mpp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,4 @@ EXPLAIN ANALYZE SELECT COUNT(*) FROM t1 GROUP BY id;
173173
+ [聚合查询的执行计划](/explain-aggregation.md)
174174
+ [视图查询的执行计划](/explain-views.md)
175175
+ [分区查询的执行计划](/explain-partitions.md)
176-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
176+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-overview.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Records: 2 Duplicates: 0 Warnings: 0
4747
+ [聚合查询的执行计划](/explain-aggregation.md)
4848
+ [视图查询的执行计划](/explain-views.md)
4949
+ [分区查询的执行计划](/explain-partitions.md)
50-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
50+
+ [索引合并查询的执行计划](/explain-index-merge.md)
5151

5252
## 解读 EXPLAIN 的返回结果
5353

explain-partitions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ EXPLAIN SELECT COUNT(*) FROM t1 WHERE YEAR(d) = 2017;
139139
+ [子查询的执行计划](/explain-subqueries.md)
140140
+ [聚合查询的执行计划](/explain-aggregation.md)
141141
+ [视图查询的执行计划](/explain-views.md)
142-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
142+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-subqueries.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,4 @@ EXPLAIN SELECT * FROM t3 WHERE t1_id NOT IN (SELECT id FROM t1 WHERE int_col < 1
156156
+ [聚合查询的执行计划](/explain-aggregation.md)
157157
+ [视图查询的执行计划](/explain-views.md)
158158
+ [分区查询的执行计划](/explain-partitions.md)
159-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
159+
+ [索引合并查询的执行计划](/explain-index-merge.md)

explain-views.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@ Query OK, 0 rows affected (2 min 31.20 sec)
116116
+ [子查询的执行计划](/explain-subqueries.md)
117117
+ [聚合查询的执行计划](/explain-aggregation.md)
118118
+ [分区查询的执行计划](/explain-partitions.md)
119-
+ [开启 IndexMerge 查询的执行计划](/explain-index-merge.md)
119+
+ [索引合并查询的执行计划](/explain-index-merge.md)

optimizer-hints.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ SELECT /*+ USE_INDEX_MERGE(t1, idx_a, idx_b, idx_c) */ * FROM t1 WHERE t1.a > 10
226226
目前该 Hint 生效的条件较为苛刻,包括:
227227

228228
- 如果查询有除了全表扫以外的单索引扫描方式可以选择,优化器不会选择 index merge;
229-
- 如果查询在显式事务里,且该条查询之前的语句已经涉及写入,优化器不会选择 index merge;
230229

231230
## 查询范围生效的 Hint
232231

@@ -252,7 +251,8 @@ SELECT /*+ NO_INDEX_MERGE() */ * FROM t WHERE t.a > 0 or t.b > 0;
252251

253252
> **注意:**
254253
>
255-
> `NO_INDEX_MERGE` 优先级高于 `USE_INDEX_MERGE`,当这两类 Hint 同时存在时,`USE_INDEX_MERGE` 不会生效。
254+
> - `NO_INDEX_MERGE` 优先级高于 `USE_INDEX_MERGE`,当这两类 Hint 同时存在时,`USE_INDEX_MERGE` 不会生效。
255+
> - 当存在子查询时,`NO_INDEX_MERGE` 放在最外层才能生效。
256256
257257
### USE_TOJA(boolean_value)
258258

system-variables.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,15 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
610610
### `tidb_enable_index_merge` <span class="version-mark">从 v4.0 版本开始引入</span>
611611

612612
- 作用域:SESSION | GLOBAL
613-
- 默认值:`OFF`
613+
- 默认值:`ON`
614614
- 这个变量用于控制是否开启 index merge 功能。
615615

616+
> **注意:**
617+
>
618+
> - 当集群从 v4.0.0 以下版本升级到 v5.4.0 及以上版本时,该变量开关默认关闭,防止升级后计划发生变化导致回退。
619+
> - 当集群从 v4.0.0 及以上版本升级到 v5.4.0 及以上版本时,该变量开关保持升级前的状态。
620+
> - 对于 v5.4.0 及以上版本的新建集群,该变量开关默认开启。
621+
616622
### `tidb_enable_list_partition` <span class="version-mark">从 v5.0 版本开始引入</span>
617623

618624
> **警告:**

0 commit comments

Comments
 (0)