Skip to content

Commit

Permalink
add system variable tidb_opt_range_max_size (#11239)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyifangreeneyes authored Nov 7, 2022
1 parent 98c1642 commit 4d974f4
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,136 @@ explain select * from t where age=5;
- 默认值:`OFF`
- 指定是否允许优化器将 `Projection` 算子下推到 TiKV 或者 TiFlash。
### `tidb_opt_range_max_size` <span class="version-mark">从 v6.4.0 版本开始引入</span>
- 作用域:SESSION | GLOBAL
- 是否持久化到集群:是
- 默认值:`67108864` (64 MiB)
- 取值范围:`[0, 9223372036854775807]`
- 单位:字节
- 该变量用于指定优化器构造扫描范围的内存用量上限。当该变量为 `0` 时,表示对扫描范围没有内存限制。如果构造精确的扫描范围会超出内存用量限制,优化器会使用更宽松的扫描范围(例如 `[[NULL,+inf]]`)。如果执行计划中未使用精确的扫描范围,可以调大该变量的值让优化器构造精确的扫描范围。
该变量的使用示例如下:
<details>
<summary><code>tidb_opt_range_max_size</code> 使用示例</summary>
查看该变量的默认值,即优化器构造扫描范围最多使用 64 MiB 内存。
```sql
SELECT @@tidb_opt_range_max_size;
```
```sql
+----------------------------+
| @@tidb_opt_range_max_size |
+----------------------------+
| 67108864 |
+----------------------------+
1 row in set (0.01 sec)
```
```sql
EXPLAIN SELECT * FROM t use index (idx) WHERE a IN (10,20,30) AND b IN (40,50,60);
```
64 MiB 的内存最大限制约束下,优化器构造出精确的扫描范围 `[10 40,10 40], [10 50,10 50], [10 60,10 60], [20 40,20 40], [20 50,20 50], [20 60,20 60], [30 40,30 40], [30 50,30 50], [30 60,30 60]`,见如下执行计划返回结果。
```sql
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IndexLookUp_7 | 0.90 | root | | |
| ├─IndexRangeScan_5(Build) | 0.90 | cop[tikv] | table:t, index:idx(a, b) | range:[10 40,10 40], [10 50,10 50], [10 60,10 60], [20 40,20 40], [20 50,20 50], [20 60,20 60], [30 40,30 40], [30 50,30 50], [30 60,30 60], keep order:false, stats:pseudo |
| └─TableRowIDScan_6(Probe) | 0.90 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
```
现将优化器构造扫描范围的内存用量上限设为 1500 字节。
```sql
SET @@tidb_opt_range_max_size = 1500;
```
```sql
Query OK, 0 rows affected (0.00 sec)
```
```sql
EXPLAIN SELECT * FROM t USE INDEX (idx) WHERE a IN (10,20,30) AND b IN (40,50,60);
```
1500 字节内存的最大限制约束下,优化器构造出了更宽松的扫描范围 `[10,10], [20,20], [30,30]`,并用 warning 提示用户构造精确的扫描范围所需的内存用量超出了 `tidb_opt_range_max_size` 的限制。
```sql
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------+
| IndexLookUp_8 | 0.09 | root | | |
| ├─Selection_7(Build) | 0.09 | cop[tikv] | | in(test.t.b, 40, 50, 60) |
| │ └─IndexRangeScan_5 | 30.00 | cop[tikv] | table:t, index:idx(a, b) | range:[10,10], [20,20], [30,30], keep order:false, stats:pseudo |
| └─TableRowIDScan_6(Probe) | 0.09 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+-------------------------------+---------+-----------+--------------------------+-----------------------------------------------------------------+
4 rows in set, 1 warning (0.00 sec)
```
```sql
SHOW WARNINGS;
```
```sql
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1105 | Memory capacity of 1500 bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
再将优化器构造扫描范围的内存用量上限设为 100 字节。
```sql
set @@tidb_opt_range_max_size = 100;
```
```sql
Query OK, 0 rows affected (0.00 sec)
```
```sql
EXPLAIN SELECT * FROM t USE INDEX (idx) WHERE a IN (10,20,30) AND b IN (40,50,60);
```
100 字节的内存最大限制约束下,优化器选择了 `IndexFullScan`,并用 warning 提示用户构造精确的扫描范围所需的内存超出了 `tidb_opt_range_max_size` 的限制。
```sql
+-------------------------------+----------+-----------+--------------------------+----------------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------------+----------+-----------+--------------------------+----------------------------------------------------+
| IndexLookUp_8 | 8000.00 | root | | |
| ├─Selection_7(Build) | 8000.00 | cop[tikv] | | in(test.t.a, 10, 20, 30), in(test.t.b, 40, 50, 60) |
| │ └─IndexFullScan_5 | 10000.00 | cop[tikv] | table:t, index:idx(a, b) | keep order:false, stats:pseudo |
| └─TableRowIDScan_6(Probe) | 8000.00 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+-------------------------------+----------+-----------+--------------------------+----------------------------------------------------+
4 rows in set, 1 warning (0.00 sec)
```
```sql
SHOW WARNINGS;
```
```sql
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1105 | Memory capacity of 100 bytes for 'tidb_opt_range_max_size' exceeded when building ranges. Less accurate ranges such as full range are chosen |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
</details>
### `tidb_opt_scan_factor`
- 作用域:SESSION | GLOBAL
Expand Down

0 comments on commit 4d974f4

Please sign in to comment.