Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions reference/mysql-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ TiDB 支持常用的 MySQL 内建函数,但是不是所有的函数都已经

+ Add Index
- 不支持同时创建多个索引
- 不支持 `VISIBLE/INVISIBLE` 的索引
- 其他类型的 Index Type (HASH/BTREE/RTREE) 只有语法支持,功能不支持
+ Add Column
- 不支持同时创建多个列
Copy link
Contributor Author

@Deardrops Deardrops May 9, 2020

Choose a reason for hiding this comment

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

This feature has implemented in TiDB, this line should be removed.

- 不支持将新创建的列设为主键或唯一索引,也不支持将此列设成 AUTO_INCREMENT 属性
+ Drop Column: 不支持删除主键列或索引列
+ Change/Modify Column
Expand Down
1 change: 1 addition & 0 deletions reference/sql/statements/add-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
* [CREATE INDEX](/reference/sql/statements/create-index.md)
* [DROP INDEX](/reference/sql/statements/drop-index.md)
* [RENAME INDEX](/reference/sql/statements/rename-index.md)
* [ALTER INDEX](/reference/sql/statements/alter-index.md)
* [ADD COLUMN](/reference/sql/statements/add-column.md)
* [CREATE TABLE](/reference/sql/statements/create-table.md)
* [EXPLAIN](/reference/sql/statements/explain.md)
190 changes: 190 additions & 0 deletions reference/sql/statements/alter-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
---
title: ALTER INDEX
summary: TiDB 数据库中 ALTER INDEX 的使用概况。
category: reference
---

# ALTER INDEX

`ALTER INDEX` 语句用于修改索引的可见性,可以将索引设置为 `Visible` 或者 `Invisible`。设置为 `Invisible` 的索引将无法被优化器使用。

## 语法

{{< copyable "sql" >}}

```sql
ALTER TABLE [table_name] ALTER INDEX [index_name] {VISIBLE | INVISIBLE}
```

## 示例

可以通过 `ALTER TABLE ... ALTER INDEX ...` 语句,修改索引的可见性:

{{< copyable "sql" >}}

```sql
CREATE TABLE t1 (c1 INT, UNIQUE(c1));
ALTER TABLE t1 ALTER INDEX c1 INVISIBLE;
```

```sql
Query OK, 0 rows affected (0.02 sec)
```

{{< copyable "sql" >}}

```sql
SHOW CREATE TABLE t1;
```

```sql
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`c1` int(11) DEFAULT NULL,
UNIQUE KEY `c1` (`c1`) /*!80000 INVISIBLE */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

## 不可见索引

不可见索引 (Invisible Indexes) 是 MySQL 8.0 引入的新功能,将一个索引设置为不可见,使优化器不会再使用这条索引。这个功能可以方便地验证使用或者不使用一条索引的查询计划,避免了 `Drop index` 或 `Add index` 这种资源消耗较多的操作。

{{< copyable "sql" >}}

```sql
CREATE TABLE t1 (c1 INT, c2 INT, UNIQUE(c2));
CREATE UNIQUE INDEX c1 ON t1 (c1) INVISIBLE;
```

可以通过 `Create Table` 语句查看,不可见的索引会用 `/*!80000 INVISIBLE */` 标识出:

{{< copyable "sql" >}}

```sql
SHOW CREATE TABLE t1;
```

```sql
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
UNIQUE KEY `c2` (`c2`),
UNIQUE KEY `c1` (`c1`) /*!80000 INVISIBLE */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

优化器将无法使用 `c1` 这个**不可见的索引**:

{{< copyable "sql" >}}

```sql
EXPLAIN SELECT c1 FROM t1 ORDER BY c1;
```

```sql
+-------------------------+----------+-----------+---------------+--------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+----------+-----------+---------------+--------------------------------+
| Sort_4 | 10000.00 | root | | test.t1.c1:asc |
| └─TableReader_8 | 10000.00 | root | | data:TableFullScan_7 |
| └─TableFullScan_7 | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+---------------+--------------------------------+
3 rows in set (0.00 sec)
```

作为对比,c2 是**可见的索引**,优化器将可以使用索引:

{{< copyable "sql" >}}

```sql
EXPLAIN SELECT c2 FROM t1 ORDER BY c2;
```

```sql
+------------------------+----------+-----------+------------------------+-------------------------------+
| id | estRows | task | access object | operator info |
+------------------------+----------+-----------+------------------------+-------------------------------+
| IndexReader_13 | 10000.00 | root | | index:IndexFullScan_12 |
| └─IndexFullScan_12 | 10000.00 | cop[tikv] | table:t1, index:c2(c2) | keep order:true, stats:pseudo |
+------------------------+----------+-----------+------------------------+-------------------------------+
2 rows in set (0.00 sec)
```

即使用 SQL Hint `USE INDEX` 强制使用索引,优化器也无法使用不可见索引,否则 SQL 语句会报错:

{{< copyable "sql" >}}

```sql
SELECT * FROM t1 USE INDEX(c1);
```

```sql
ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1'
```

> **注意:**
>
> “不可见”是仅仅对优化器而言的,不可见索引仍然可以被修改或删除。

{{< copyable "sql" >}}

```sql
ALTER TABLE t1 DROP INDEX c1;
```

```sql
Query OK, 0 rows affected (0.02 sec)
```

### 限制

MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见。TiDB 兼容这条限制,将主键设置为不可见后会抛出错误。

{{< copyable "sql" >}}

```sql
CREATE TABLE t2(c1 INT, PRIMARY KEY(c1) INVISIBLE);
```

```sql
ERROR 3522 (HY000): A primary key index cannot be invisible
```

这里的**主键**包括既包括显式的主键(通过 `PRIMARY KEY` 关键字指定的主键),也包括隐式的主键。

当表中不存在显式的主键时,第一个 `UNIQUE` 索引(要求满足索引上的每一列都是 `NOT NULL`)会自动成为隐式的主键。

即不能将隐式的主键设置为不可见的。

> **注意:**
>
> TiDB 并不会实际创建一个**隐式的主键**,这个限制仅仅在行为上兼容 MySQL。

{{< copyable "sql" >}}

```sql
CREATE TABLE t2(c1 INT NOT NULL, UNIQUE(c1) INVISIBLE);
```

```
ERROR 3522 (HY000): A primary key index cannot be invisible
```

## 另请参阅

* [CREATE TABLE](/reference/sql/statements/create-table.md)
* [CREATE INDEX](/reference/sql/statements/create-index.md)
* [ADD INDEX](/reference/sql/statements/add-index.md)
* [DROP INDEX](/reference/sql/statements/drop-index.md)
* [RENAME INDEX](/reference/sql/statements/rename-index.md)
1 change: 1 addition & 0 deletions reference/sql/statements/alter-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
* [ADD INDEX](/reference/sql/statements/add-index.md)
* [DROP INDEX](/reference/sql/statements/drop-index.md)
* [RENAME INDEX](/reference/sql/statements/rename-index.md)
* [ALTER INDEX](/reference/sql/statements/alter-index.md)
* [CREATE TABLE](/reference/sql/statements/create-table.md)
* [DROP TABLE](/reference/sql/statements/drop-table.md)
* [SHOW CREATE TABLE](/reference/sql/statements/show-create-table.md)
12 changes: 12 additions & 0 deletions reference/sql/statements/create-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ CREATE INDEX idx ON t ((lower(name)));

表达式索引的语法和限制与 MySQL 相同,是通过将索引建立在隐藏的虚拟生成列 (generated virtual column) 上来实现的。因此所支持的表达式继承了虚拟生成列的所有[限制](/reference/sql/generated-columns.md#局限性)。目前,建立了索引的表达式只有在 `FIELD` 子句、`WHERE` 子句和 `ORDER BY` 子句中时,优化器才能使用表达式索引。后续将支持 `GROUP BY` 子句。

## 不可见索引

不可见索引(Invisible Indexes)是 MySQL 8.0 引入的新功能,将一个索引设置为不可见,使优化器不会再使用这条索引。

```sql
CREATE TABLE t1 (c1 INT, c2 INT, UNIQUE(c2));
CREATE UNIQUE INDEX c1 ON t1 (c1) INVISIBLE;
```

具体可以参考 [不可见索引的介绍](/reference/sql/statements/alter-index.md#不可见索引)。

## 相关 session 变量

和 `CREATE INDEX` 语句相关的全局变量有 `tidb_ddl_reorg_worker_cnt`,`tidb_ddl_reorg_batch_size` 和 `tidb_ddl_reorg_priority`,具体可以参考 [TiDB 特定系统变量](/reference/configuration/tidb-server/tidb-specific-variables.md#tidb_ddl_reorg_worker_cnt)。
Expand All @@ -175,6 +186,7 @@ CREATE INDEX idx ON t ((lower(name)));
* [ADD INDEX](/reference/sql/statements/add-index.md)
* [DROP INDEX](/reference/sql/statements/drop-index.md)
* [RENAME INDEX](/reference/sql/statements/rename-index.md)
* [ALTER INDEX](/reference/sql/statements/alter-index.md)
* [ADD COLUMN](/reference/sql/statements/add-column.md)
* [CREATE TABLE](/reference/sql/statements/create-table.md)
* [EXPLAIN](/reference/sql/statements/explain.md)
1 change: 1 addition & 0 deletions reference/sql/statements/create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| COMMENT 'string'
| {VISIBLE | INVISIBLE}
```

`index_option` 中 `KEY_BLOCK_SIZE` 目前只是语法上支持。
Expand Down
1 change: 1 addition & 0 deletions reference/sql/statements/drop-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ Query OK, 0 rows affected (0.30 sec)
* [CREATE INDEX](/reference/sql/statements/create-index.md)
* [ADD INDEX](/reference/sql/statements/add-index.md)
* [RENAME INDEX](/reference/sql/statements/rename-index.md)
* [ALTER INDEX](/reference/sql/statements/alter-index.md)
1 change: 1 addition & 0 deletions reference/sql/statements/rename-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Create Table: CREATE TABLE `t1` (
* [CREATE INDEX](/reference/sql/statements/create-index.md)
* [DROP INDEX](/reference/sql/statements/drop-index.md)
* [SHOW INDEX](/reference/sql/statements/show-index.md)
* [ALTER INDEX](/reference/sql/statements/alter-index.md)
42 changes: 24 additions & 18 deletions reference/sql/statements/show-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ SHOW INDEXES FROM t1;
```

```
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES
| NULL |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | | YES
| NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)
```

Expand All @@ -65,12 +67,14 @@ SHOW INDEX FROM t1;
```

```
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES
| NULL |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | | YES
| NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)
```

Expand All @@ -81,12 +85,14 @@ SHOW KEYS FROM t1;
```

```
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| t1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES
| NULL |
| t1 | 1 | col1 | 1 | col1 | A | 0 | NULL | NULL | YES | BTREE | | | YES
| NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)
```

Expand Down
42 changes: 22 additions & 20 deletions reference/system-databases/information-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,26 +469,28 @@ desc statistics;
```

```
+---------------|---------------------|------|------|---------|-------+
| Field | Type | Null | Key | Default | Extra |
+---------------|---------------------|------|------|---------|-------+
| TABLE_CATALOG | varchar(512) | YES | | NULL | |
| TABLE_SCHEMA | varchar(64) | YES | | NULL | |
| TABLE_NAME | varchar(64) | YES | | NULL | |
| NON_UNIQUE | varchar(1) | YES | | NULL | |
| INDEX_SCHEMA | varchar(64) | YES | | NULL | |
| INDEX_NAME | varchar(64) | YES | | NULL | |
| SEQ_IN_INDEX | bigint(2) UNSIGNED | YES | | NULL | |
| COLUMN_NAME | varchar(21) | YES | | NULL | |
| COLLATION | varchar(1) | YES | | NULL | |
| CARDINALITY | bigint(21) UNSIGNED | YES | | NULL | |
| SUB_PART | bigint(3) UNSIGNED | YES | | NULL | |
| PACKED | varchar(10) | YES | | NULL | |
| NULLABLE | varchar(3) | YES | | NULL | |
| INDEX_TYPE | varchar(16) | YES | | NULL | |
| COMMENT | varchar(16) | YES | | NULL | |
| INDEX_COMMENT | varchar(1024) | YES | | NULL | |
+---------------|---------------------|------|------|---------|-------+
+---------------+---------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+------+---------+-------+
| TABLE_CATALOG | varchar(512) | YES | | NULL | |
| TABLE_SCHEMA | varchar(64) | YES | | NULL | |
| TABLE_NAME | varchar(64) | YES | | NULL | |
| NON_UNIQUE | varchar(1) | YES | | NULL | |
| INDEX_SCHEMA | varchar(64) | YES | | NULL | |
| INDEX_NAME | varchar(64) | YES | | NULL | |
| SEQ_IN_INDEX | bigint(2) | YES | | NULL | |
| COLUMN_NAME | varchar(21) | YES | | NULL | |
| COLLATION | varchar(1) | YES | | NULL | |
| CARDINALITY | bigint(21) | YES | | NULL | |
| SUB_PART | bigint(3) | YES | | NULL | |
| PACKED | varchar(10) | YES | | NULL | |
| NULLABLE | varchar(3) | YES | | NULL | |
| INDEX_TYPE | varchar(16) | YES | | NULL | |
| COMMENT | varchar(16) | YES | | NULL | |
| INDEX_COMMENT | varchar(1024) | YES | | NULL | |
| IS_VISIBLE | varchar(3) | YES | | NULL | |
| Expression | varchar(64) | YES | | NULL | |
+---------------+---------------+------+------+---------+-------+
```

下列语句是等价的:
Expand Down