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

docs: fix expression index's document again #6295

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 27 additions & 4 deletions sql-statements/sql-statement-create-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,28 @@ Query OK, 0 rows affected (0.31 sec)

In some scenarios, the filtering condition of a query is based on a certain expression. In these scenarios, the query performance is relatively poor because ordinary indexes cannot take effect, the query can only be executed by scanning the entire table. The expression index is a type of special index that can be created on an expression. Once an expression index is created, TiDB can use the index for the expression-based query, which significantly improves the query performance.

For example, if you want to create an index based on `col1+cols2`, execute the following SQL statement:
For example, if you want to create an index based on `lower(col1)`, execute the following SQL statement:

{{< copyable "sql" >}}

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

Or you can execute the following equivalent statement:

{{< copyable "sql" >}}

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

You can also specify the expression index when you create the table:

{{< 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)));
```

You can drop an expression index in the same way as dropping an ordinary index:
Expand All @@ -139,6 +139,29 @@ DROP INDEX idx1 ON t1;
```

> **Note:**
>
> Expression index involves various kinds of expressions. To ensure correctness, only the fully tested functions are allowed to be used to create an expression index currently. That is, only these functions are allowed to be included in expressions in a production environment. You can get these functions by querying
en-jin19 marked this conversation as resolved.
Show resolved Hide resolved
`tidb_allow_function_for_expression_index` variable. In later versions, these functions will be added more.
en-jin19 marked this conversation as resolved.
Show resolved Hide resolved
>
> {{< 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)
> ```
>
> For the functions that are not included in the returned result of the above variable, you are not recommended to use those functions in a production environment because they are currently experimental features due to incomplete testing. However, if you still want to use those functions, you can set the configuration as following in the [TiDB configuration file](/tidb-configuration-file.md#allow-expression-index-new-in-v400):
en-jin19 marked this conversation as resolved.
Show resolved Hide resolved
>
> {{< copyable "sql" >}}
>
> ```sql
> allow-expression-index = true
> ```
>
> An expression index cannot be created on a primary key.
>
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;
- Default value: ""
- This variable is used to specify a list of storage engines that might fall back to TiKV. If the execution of a SQL statement fails due to a failure of the specified storage engine in the list, TiDB retries executing this SQL statement with TiKV. This variable can be set to "" or "tiflash". When this variable is set to "tiflash", if the execution of a SQL statement fails due to a failure of TiFlash, TiDB retries executing this SQL statement with TiKV.

### tidb_allow_function_for_expression_index <span class="version-mark">New in v5.2.0</span>

- Scope: NONE
- This variable is used to show the functions that are allowed to be used to create expression indexes.
en-jin19 marked this conversation as resolved.
Show resolved Hide resolved

### tidb_allow_mpp <span class="version-mark">New in v5.0</span>

- Scope: 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 @@ -642,3 +642,12 @@ For pessimistic transaction usage, refer to [TiDB Pessimistic Transaction Mode](

+ Controls whether the [`INFORMATION_SCHEMA.DEADLOCKS`](/information-schema/information-schema-deadlocks.md) table collects the information of retryable deadlock errors. For the description of retryable deadlock errors, see [Retryable deadlock errors](/information-schema/information-schema-deadlocks.md#retryable-deadlock-errors).
+ Default value: `false`

## experimental

The `experimental` section, introduced in v3.1.0, describes the configurations related to the experimental features of TiDB.

### `allow-expression-index` <span class="version-mark">New in v4.0.0</span>

+ Control whether an expression index can be created. Since TiDB v5.2.0, if the functions in an expression are safe, you can create an expression index directly based on those functions without enabling this configuration. If you want to create an expression index based on other functions, you can enable this configuration, but correctness issues might exist. By querying the `tidb_allow_function_for_expression_index` variable, you get the functions that are safe to be used to create an expression directly.
en-jin19 marked this conversation as resolved.
Show resolved Hide resolved
+ Default value: `false`