Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix expression index's document again. #6950

Merged
merged 9 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- 这个变量用于显示创建表达式索引所允许使用的函数。
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样,推荐很奇怪。这里有些歧义,推荐有“让用户用的意思”,如果不符合用户的需求,也“推荐” 的话就不合适了。


### `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