Skip to content

Commit

Permalink
fix expression index's document again. (pingcap#6950)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Aug 26, 2021
1 parent c4fd913 commit 95e9b95
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
30 changes: 26 additions & 4 deletions sql-statements/sql-statement-create-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,28 @@ Query OK, 0 rows affected (0.31 sec)

在一些场景中,查询的条件往往是基于某个表达式进行过滤。在这些场景中,一般的索引不能生效,执行查询只能遍历整个表,导致查询性能较差。表达式索引是一种特殊的索引,能将索引建立于表达式上。在创建了表达式索引后,基于表达式的查询便可以使用上索引,极大提升查询的性能。

假设要基于 `col1+cols2` 这个表达式建立索引,示例的 SQL 语句如下:
假设要基于 `lower(col1)` 这个表达式建立索引,示例的 SQL 语句如下:

{{< copyable "sql" >}}

```sql
CREATE INDEX idx1 ON t1 ((col1 + col2));
CREATE INDEX idx1 ON t1 (lower(col1));
```

或者等价的语句:

{{< copyable "sql" >}}

```sql
ALTER TABLE t1 ADD INDEX idx1((col1 + col2));
ALTER TABLE t1 ADD INDEX idx1(lower(col1));
```

还可以在建表的同时指定表达式索引:

{{< copyable "sql" >}}

```sql
CREATE TABLE t1(col1 char(10), col2 char(10), key index((col1 + col2)));
CREATE TABLE t1(col1 char(10), col2 char(10), key index(lower(col1)));
```

删除表达式索引与删除普通索引的方法一致:
Expand All @@ -186,6 +186,28 @@ DROP INDEX idx1 ON t1;
```

> **注意:**
>
> 表达式索引涉及众多表达式。为了确保正确性,当前仅允许经充分测试的一部分函数用于创建表达式索引,即生产环境中仅允许表达式中包含这些函数。这些函数可以通过查询变量 `tidb_allow_function_for_expression_index` 得到。在后续版本中,这些函数会持续增加。
>
> {{< copyable "sql" >}}
>
> ```sql
> mysql> select @@tidb_allow_function_for_expression_index;
> +--------------------------------------------+
> | @@tidb_allow_function_for_expression_index |
> +--------------------------------------------+
> | lower, md5, reverse, upper, vitess_hash |
> +--------------------------------------------+
> 1 row in set (0.00 sec)
> ```
>
> 对于以上变量返回结果之外的函数,由于未完成充分测试,当前仍为实验特性,不建议在生产环境中使用。如果仍然希望使用,可以在 [TiDB 配置文件](/tidb-configuration-file.md#allow-expression-index-从-v400-版本开始引入)中进行以下设置:
>
> {{< copyable "sql" >}}
>
> ```sql
> allow-expression-index = true
> ```
>
> 表达式索引不能为主键。
>
Expand Down
5 changes: 5 additions & 0 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ mysql> SELECT * FROM t1;
- 默认值:""
- 这个变量表示将 TiKV 作为备用存储引擎的存储引擎列表。当该列表中的存储引擎发生故障导致 SQL 语句执行失败时,TiDB 会使用 TiKV 作为存储引擎再次执行该 SQL 语句。目前支持设置该变量为 "" 或者 "tiflash"。如果设置该变量为 "tiflash",当 TiFlash 发生故障导致 SQL 语句执行失败时,TiDB 会使用 TiKV 作为存储引擎再次执行该 SQL 语句。

### `tidb_allow_function_for_expression_index` <span class="version-mark">从 v5.2.0 版本开始引入</span>

- 作用域:NONE
- 这个变量用于显示创建表达式索引所允许使用的函数。

### `tidb_allow_mpp` <span class="version-mark">从 v5.0 版本开始引入</span>

- 作用域:SESSION | GLOBAL
Expand Down
9 changes: 9 additions & 0 deletions tidb-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,12 @@ TiDB 服务状态相关配置。

+ 控制 [`INFORMATION_SCHEMA.DEADLOCKS`](/information-schema/information-schema-deadlocks.md) 表中是否收集可重试的死锁错误信息。详见 `DEADLOCKS` 表文档的[可重试的死锁错误](/information-schema/information-schema-deadlocks.md#可重试的死锁错误)小节。
+ 默认值:false

## experimental

experimental 部分为 TiDB 实验功能相关的配置。该部分从 v3.1.0 开始引入。

### `allow-expression-index` <span class="version-mark">从 v4.0.0 版本开始引入</span>

+ 用于控制是否能创建表达式索引。自 v5.2.0 版本起,如果表达式中的函数是安全的,你可以直接基于该函数创建表达式索引,不需要打开该配置项。如果要创建基于其他函数的表达式索引,可以打开该配置项,但可能存在正确性问题。通过查询 `tidb_allow_function_for_expression_index` 变量可得到能直接用于创建表达式的安全函数。
+ 默认值:false

0 comments on commit 95e9b95

Please sign in to comment.