From 163992676c879068df6be298b28fdbe03d990467 Mon Sep 17 00:00:00 2001 From: deardrops Date: Sat, 9 May 2020 12:56:48 +0800 Subject: [PATCH 1/4] sql: introduce invisible index --- reference/mysql-compatibility.md | 2 - reference/sql/statements/add-index.md | 1 + reference/sql/statements/alter-index.md | 181 ++++++++++++++++++ reference/sql/statements/alter-table.md | 1 + reference/sql/statements/create-index.md | 12 ++ reference/sql/statements/create-table.md | 1 + reference/sql/statements/drop-index.md | 1 + reference/sql/statements/rename-index.md | 1 + reference/sql/statements/show-indexes.md | 42 ++-- .../system-databases/information-schema.md | 42 ++-- 10 files changed, 244 insertions(+), 40 deletions(-) create mode 100644 reference/sql/statements/alter-index.md diff --git a/reference/mysql-compatibility.md b/reference/mysql-compatibility.md index d2e4c61dfdf2..5a8a06a0aaa0 100644 --- a/reference/mysql-compatibility.md +++ b/reference/mysql-compatibility.md @@ -105,10 +105,8 @@ TiDB 支持常用的 MySQL 内建函数,但是不是所有的函数都已经 + Add Index - 不支持同时创建多个索引 - - 不支持 `VISIBLE/INVISIBLE` 的索引 - 其他类型的 Index Type (HASH/BTREE/RTREE) 只有语法支持,功能不支持 + Add Column - - 不支持同时创建多个列 - 不支持将新创建的列设为主键或唯一索引,也不支持将此列设成 AUTO_INCREMENT 属性 + Drop Column: 不支持删除主键列或索引列 + Change/Modify Column diff --git a/reference/sql/statements/add-index.md b/reference/sql/statements/add-index.md index ffe23024b165..76bfe6bccf62 100644 --- a/reference/sql/statements/add-index.md +++ b/reference/sql/statements/add-index.md @@ -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) diff --git a/reference/sql/statements/alter-index.md b/reference/sql/statements/alter-index.md new file mode 100644 index 000000000000..8841209fe957 --- /dev/null +++ b/reference/sql/statements/alter-index.md @@ -0,0 +1,181 @@ +--- +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; +``` + +``` +Query OK, 0 rows affected (0.02 sec) + +``` + +{{< copyable "sql" >}} + +```sql +SHOW CREATE TABLE t1; +``` + +``` ++-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| 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; +``` + +``` ++-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| 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) +``` + +优化器将无法使用**不可见的索引**: + +{{< copyable "sql" >}} + +```sql +EXPLAIN SELECT c1 FROM t1 ORDER BY c1; +``` + +``` ++-------------------------+----------+-----------+---------------+--------------------------------+ +| 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; +``` + +``` ++------------------------+----------+-----------+------------------------+-------------------------------+ +| 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); +``` + +``` +ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1' +``` + +注意,“不可见”是仅仅对优化器而言的,不可见索引仍然可以被修改或删除。 + +{{< copyable "sql" >}} + +```sql +ALTER TABLE t1 DROP INDEX c1; +``` + +``` +Query OK, 0 rows affected (0.02 sec) +``` + +### 限制 + +MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见的。TiDB 兼容这条限制,这种情况会会抛出错误。 + +{{< copyable "sql" >}} + +```sql +CREATE TABLE t2(c1 INT, PRIMARY KEY(c1) INVISIBLE); +``` + +``` +ERROR 3522 (HY000): A primary key index cannot be invisible +``` + +这里的**主键**包括隐式的主键,隐式主键指的是当表中不存在显式的主键时,第一个 UNIQUE 索引(要求满足索引上的每一列都是 NOT NULL)会成为隐式的主键。 + +即隐式的主键也不能被设置为不可见的。 + +{{< 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) \ No newline at end of file diff --git a/reference/sql/statements/alter-table.md b/reference/sql/statements/alter-table.md index 0a4756e207e7..c9edf244fb47 100644 --- a/reference/sql/statements/alter-table.md +++ b/reference/sql/statements/alter-table.md @@ -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) diff --git a/reference/sql/statements/create-index.md b/reference/sql/statements/create-index.md index bd66e1657154..de24cf96250c 100644 --- a/reference/sql/statements/create-index.md +++ b/reference/sql/statements/create-index.md @@ -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)。 @@ -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) diff --git a/reference/sql/statements/create-table.md b/reference/sql/statements/create-table.md index d45b0ab5d397..719a6b7fc6e8 100644 --- a/reference/sql/statements/create-table.md +++ b/reference/sql/statements/create-table.md @@ -172,6 +172,7 @@ index_option: KEY_BLOCK_SIZE [=] value | index_type | COMMENT 'string' + | {VISIBLE | INVISIBLE} ``` `index_option` 中 `KEY_BLOCK_SIZE` 目前只是语法上支持。 diff --git a/reference/sql/statements/drop-index.md b/reference/sql/statements/drop-index.md index 5e09d998f926..0839351af8ce 100644 --- a/reference/sql/statements/drop-index.md +++ b/reference/sql/statements/drop-index.md @@ -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) diff --git a/reference/sql/statements/rename-index.md b/reference/sql/statements/rename-index.md index da02d789ef11..c97ee4aeca94 100644 --- a/reference/sql/statements/rename-index.md +++ b/reference/sql/statements/rename-index.md @@ -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) diff --git a/reference/sql/statements/show-indexes.md b/reference/sql/statements/show-indexes.md index 12eadf96dfe0..dd458a82a78c 100644 --- a/reference/sql/statements/show-indexes.md +++ b/reference/sql/statements/show-indexes.md @@ -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) ``` @@ -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) ``` @@ -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) ``` diff --git a/reference/system-databases/information-schema.md b/reference/system-databases/information-schema.md index 1af2decf7fb5..c0947802c7da 100644 --- a/reference/system-databases/information-schema.md +++ b/reference/system-databases/information-schema.md @@ -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 | | ++---------------+---------------+------+------+---------+-------+ ``` 下列语句是等价的: From dc3f1341c84d7a70a54a7fac5d07d9b19ae183c8 Mon Sep 17 00:00:00 2001 From: deardrops Date: Sat, 9 May 2020 15:02:08 +0800 Subject: [PATCH 2/4] address comment --- reference/sql/statements/alter-index.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/reference/sql/statements/alter-index.md b/reference/sql/statements/alter-index.md index 8841209fe957..93fb757b48c6 100644 --- a/reference/sql/statements/alter-index.md +++ b/reference/sql/statements/alter-index.md @@ -160,9 +160,15 @@ CREATE TABLE t2(c1 INT, PRIMARY KEY(c1) INVISIBLE); ERROR 3522 (HY000): A primary key index cannot be invisible ``` -这里的**主键**包括隐式的主键,隐式主键指的是当表中不存在显式的主键时,第一个 UNIQUE 索引(要求满足索引上的每一列都是 NOT NULL)会成为隐式的主键。 +这里的**主键**包括既包括显式的主键(通过 PRIMARY KEY 关键字指定的主键),也包括隐式的主键。 -即隐式的主键也不能被设置为不可见的。 +当表中不存在显式的主键时,第一个 UNIQUE 索引(要求满足索引上的每一列都是 NOT NULL)会自动成为隐式的主键。 + +即不能将隐式的主键设置为不可见的。 + +> **注意:** +> +> TiDB 并不会实际创建一个**隐式的主键**,这个限制仅仅在行为上兼容 MySQL。 {{< copyable "sql" >}} From c74775bb08baf97372fc2644e7536f80af80af73 Mon Sep 17 00:00:00 2001 From: deardrops Date: Sat, 9 May 2020 18:20:55 +0800 Subject: [PATCH 3/4] address comment --- reference/sql/statements/alter-index.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/reference/sql/statements/alter-index.md b/reference/sql/statements/alter-index.md index 93fb757b48c6..55f69a3c9fe6 100644 --- a/reference/sql/statements/alter-index.md +++ b/reference/sql/statements/alter-index.md @@ -29,7 +29,6 @@ ALTER TABLE t1 ALTER INDEX c1 INVISIBLE; ``` Query OK, 0 rows affected (0.02 sec) - ``` {{< copyable "sql" >}} @@ -134,7 +133,9 @@ SELECT * FROM t1 USE INDEX(c1); ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1' ``` -注意,“不可见”是仅仅对优化器而言的,不可见索引仍然可以被修改或删除。 +> **注意:** +> +> “不可见”是仅仅对优化器而言的,不可见索引仍然可以被修改或删除。 {{< copyable "sql" >}} @@ -148,7 +149,7 @@ Query OK, 0 rows affected (0.02 sec) ### 限制 -MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见的。TiDB 兼容这条限制,这种情况会会抛出错误。 +MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见的。TiDB 兼容这条限制,这种情况会抛出错误。 {{< copyable "sql" >}} @@ -184,4 +185,6 @@ 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) \ No newline at end of file +* [ADD INDEX](/reference/sql/statements/add-index.md) +* [DROP INDEX](/reference/sql/statements/drop-index.md) +* [RENAME INDEX](/reference/sql/statements/rename-index.md) \ No newline at end of file From d37ce0048af84656a37243ee18db76ab5ba3307c Mon Sep 17 00:00:00 2001 From: TomShawn <41534398+TomShawn@users.noreply.github.com> Date: Mon, 11 May 2020 12:21:02 +0800 Subject: [PATCH 4/4] refine format --- reference/sql/statements/alter-index.md | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/reference/sql/statements/alter-index.md b/reference/sql/statements/alter-index.md index 55f69a3c9fe6..0b0c2f1b1c4b 100644 --- a/reference/sql/statements/alter-index.md +++ b/reference/sql/statements/alter-index.md @@ -6,7 +6,7 @@ category: reference # ALTER INDEX -`ALTER INDEX` 语句用于修改索引的可见性,可以将索引设置为 Visible 或者 Invisible。设置为 Invisible 的索引将无法被优化器使用。 +`ALTER INDEX` 语句用于修改索引的可见性,可以将索引设置为 `Visible` 或者 `Invisible`。设置为 `Invisible` 的索引将无法被优化器使用。 ## 语法 @@ -27,7 +27,7 @@ CREATE TABLE t1 (c1 INT, UNIQUE(c1)); ALTER TABLE t1 ALTER INDEX c1 INVISIBLE; ``` -``` +```sql Query OK, 0 rows affected (0.02 sec) ``` @@ -37,7 +37,7 @@ Query OK, 0 rows affected (0.02 sec) SHOW CREATE TABLE t1; ``` -``` +```sql +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | @@ -52,7 +52,7 @@ SHOW CREATE TABLE t1; ## 不可见索引 -不可见索引(Invisible Indexes)是 MySQL 8.0 引入的新功能,将一个索引设置为不可见,使优化器不会再使用这条索引。这个功能可以方便地验证使用或者不使用一条索引的查询计划,避免了 Drop index / Add index 这种资源消耗较多的操作。 +不可见索引 (Invisible Indexes) 是 MySQL 8.0 引入的新功能,将一个索引设置为不可见,使优化器不会再使用这条索引。这个功能可以方便地验证使用或者不使用一条索引的查询计划,避免了 `Drop index` 或 `Add index` 这种资源消耗较多的操作。 {{< copyable "sql" >}} @@ -61,7 +61,7 @@ CREATE TABLE t1 (c1 INT, c2 INT, UNIQUE(c2)); CREATE UNIQUE INDEX c1 ON t1 (c1) INVISIBLE; ``` -可以通过 Create Table 语句查看,不可见的索引会用 `/*!80000 INVISIBLE */` 标识出: +可以通过 `Create Table` 语句查看,不可见的索引会用 `/*!80000 INVISIBLE */` 标识出: {{< copyable "sql" >}} @@ -69,7 +69,7 @@ CREATE UNIQUE INDEX c1 ON t1 (c1) INVISIBLE; SHOW CREATE TABLE t1; ``` -``` +```sql +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | @@ -84,7 +84,7 @@ SHOW CREATE TABLE t1; 1 row in set (0.00 sec) ``` -优化器将无法使用**不可见的索引**: +优化器将无法使用 `c1` 这个**不可见的索引**: {{< copyable "sql" >}} @@ -92,7 +92,7 @@ SHOW CREATE TABLE t1; EXPLAIN SELECT c1 FROM t1 ORDER BY c1; ``` -``` +```sql +-------------------------+----------+-----------+---------------+--------------------------------+ | id | estRows | task | access object | operator info | +-------------------------+----------+-----------+---------------+--------------------------------+ @@ -111,7 +111,7 @@ EXPLAIN SELECT c1 FROM t1 ORDER BY c1; EXPLAIN SELECT c2 FROM t1 ORDER BY c2; ``` -``` +```sql +------------------------+----------+-----------+------------------------+-------------------------------+ | id | estRows | task | access object | operator info | +------------------------+----------+-----------+------------------------+-------------------------------+ @@ -121,7 +121,7 @@ EXPLAIN SELECT c2 FROM t1 ORDER BY c2; 2 rows in set (0.00 sec) ``` -即使用 SQL Hint `USE INDEX` 强制使用索引,优化器也无法使用不可见索引,这样的 SQL 语句会报错: +即使用 SQL Hint `USE INDEX` 强制使用索引,优化器也无法使用不可见索引,否则 SQL 语句会报错: {{< copyable "sql" >}} @@ -129,7 +129,7 @@ EXPLAIN SELECT c2 FROM t1 ORDER BY c2; SELECT * FROM t1 USE INDEX(c1); ``` -``` +```sql ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1' ``` @@ -143,13 +143,13 @@ ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1' ALTER TABLE t1 DROP INDEX c1; ``` -``` +```sql Query OK, 0 rows affected (0.02 sec) ``` ### 限制 -MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见的。TiDB 兼容这条限制,这种情况会抛出错误。 +MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见。TiDB 兼容这条限制,将主键设置为不可见后会抛出错误。 {{< copyable "sql" >}} @@ -157,13 +157,13 @@ MySQL 对不可见索引有一条限制:不能将**主键**设置为不可见 CREATE TABLE t2(c1 INT, PRIMARY KEY(c1) INVISIBLE); ``` -``` +```sql ERROR 3522 (HY000): A primary key index cannot be invisible ``` -这里的**主键**包括既包括显式的主键(通过 PRIMARY KEY 关键字指定的主键),也包括隐式的主键。 +这里的**主键**包括既包括显式的主键(通过 `PRIMARY KEY` 关键字指定的主键),也包括隐式的主键。 -当表中不存在显式的主键时,第一个 UNIQUE 索引(要求满足索引上的每一列都是 NOT NULL)会自动成为隐式的主键。 +当表中不存在显式的主键时,第一个 `UNIQUE` 索引(要求满足索引上的每一列都是 `NOT NULL`)会自动成为隐式的主键。 即不能将隐式的主键设置为不可见的。 @@ -187,4 +187,4 @@ ERROR 3522 (HY000): A primary key index cannot be invisible * [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) \ No newline at end of file +* [RENAME INDEX](/reference/sql/statements/rename-index.md)