ALTER INDEX

ALTER INDEX 语句用于修改索引的可见性,可以将索引设置为 Visible 或者 Invisible。设置为 Invisible 的索引即不可见索引 (Invisible Index) 由 DML 语句维护,不会被查询优化器使用。

语法图

  1. AlterTableStmt ::=
  2. 'ALTER' IgnoreOptional 'TABLE' TableName ( AlterTableSpecListOpt AlterTablePartitionOpt | 'ANALYZE' 'PARTITION' PartitionNameList ( 'INDEX' IndexNameList )? AnalyzeOptionListOpt )
  3. AlterTableSpec ::=
  4. TableOptionList
  5. | 'SET' 'TIFLASH' 'REPLICA' LengthNum LocationLabelList
  6. | 'CONVERT' 'TO' CharsetKw ( CharsetName | 'DEFAULT' ) OptCollate
  7. | 'ADD' ( ColumnKeywordOpt IfNotExists ( ColumnDef ColumnPosition | '(' TableElementList ')' ) | Constraint | 'PARTITION' IfNotExists NoWriteToBinLogAliasOpt ( PartitionDefinitionListOpt | 'PARTITIONS' NUM ) )
  8. | ( ( 'CHECK' | 'TRUNCATE' ) 'PARTITION' | ( 'OPTIMIZE' | 'REPAIR' | 'REBUILD' ) 'PARTITION' NoWriteToBinLogAliasOpt ) AllOrPartitionNameList
  9. | 'COALESCE' 'PARTITION' NoWriteToBinLogAliasOpt NUM
  10. | 'DROP' ( ColumnKeywordOpt IfExists ColumnName RestrictOrCascadeOpt | 'PRIMARY' 'KEY' | 'PARTITION' IfExists PartitionNameList | ( KeyOrIndex IfExists | 'CHECK' ) Identifier | 'FOREIGN' 'KEY' IfExists Symbol )
  11. | 'EXCHANGE' 'PARTITION' Identifier 'WITH' 'TABLE' TableName WithValidationOpt
  12. | ( 'IMPORT' | 'DISCARD' ) ( 'PARTITION' AllOrPartitionNameList )? 'TABLESPACE'
  13. | 'REORGANIZE' 'PARTITION' NoWriteToBinLogAliasOpt ReorganizePartitionRuleOpt
  14. | 'ORDER' 'BY' AlterOrderItem ( ',' AlterOrderItem )*
  15. | ( 'DISABLE' | 'ENABLE' ) 'KEYS'
  16. | ( 'MODIFY' ColumnKeywordOpt IfExists | 'CHANGE' ColumnKeywordOpt IfExists ColumnName ) ColumnDef ColumnPosition
  17. | 'ALTER' ( ColumnKeywordOpt ColumnName ( 'SET' 'DEFAULT' ( SignedLiteral | '(' Expression ')' ) | 'DROP' 'DEFAULT' ) | 'CHECK' Identifier EnforcedOrNot | 'INDEX' Identifier IndexInvisible )
  18. | 'RENAME' ( ( 'COLUMN' | KeyOrIndex ) Identifier 'TO' Identifier | ( 'TO' | '='? | 'AS' ) TableName )
  19. | LockClause
  20. | AlgorithmClause
  21. | 'FORCE'
  22. | ( 'WITH' | 'WITHOUT' ) 'VALIDATION'
  23. | 'SECONDARY_LOAD'
  24. | 'SECONDARY_UNLOAD'
  25. IndexInvisible ::=
  26. 'VISIBLE'
  27. | 'INVISIBLE'

示例

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

  1. CREATE TABLE t1 (c1 INT, UNIQUE(c1));
  2. ALTER TABLE t1 ALTER INDEX c1 INVISIBLE;
  1. Query OK, 0 rows affected (0.02 sec)
  1. SHOW CREATE TABLE t1;
  1. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  2. | Table | Create Table
  3. |
  4. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | t1 | CREATE TABLE `t1` (
  6. `c1` int(11) DEFAULT NULL,
  7. UNIQUE KEY `c1` (`c1`) /*!80000 INVISIBLE */
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
  9. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  10. 1 row in set (0.00 sec)

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

  1. EXPLAIN SELECT c1 FROM t1 ORDER BY c1;
  1. +-------------------------+----------+-----------+---------------+--------------------------------+
  2. | id | estRows | task | access object | operator info |
  3. +-------------------------+----------+-----------+---------------+--------------------------------+
  4. | Sort_4 | 10000.00 | root | | test.t1.c1:asc |
  5. | └─TableReader_8 | 10000.00 | root | | data:TableFullScan_7 |
  6. | └─TableFullScan_7 | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
  7. +-------------------------+----------+-----------+---------------+--------------------------------+
  8. 3 rows in set (0.00 sec)

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

  1. EXPLAIN SELECT c2 FROM t1 ORDER BY c2;
  1. +------------------------+----------+-----------+------------------------+-------------------------------+
  2. | id | estRows | task | access object | operator info |
  3. +------------------------+----------+-----------+------------------------+-------------------------------+
  4. | IndexReader_13 | 10000.00 | root | | index:IndexFullScan_12 |
  5. | └─IndexFullScan_12 | 10000.00 | cop[tikv] | table:t1, index:c2(c2) | keep order:true, stats:pseudo |
  6. +------------------------+----------+-----------+------------------------+-------------------------------+
  7. 2 rows in set (0.00 sec)

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

  1. SELECT * FROM t1 USE INDEX(c1);
  1. ERROR 1176 (42000): Key 'c1' doesn't exist in table 't1'

注意:

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

  1. ALTER TABLE t1 DROP INDEX c1;
  1. Query OK, 0 rows affected (0.02 sec)

MySQL 兼容性

  • TiDB 中的不可见索引是基于 MySQL 8.0 中的同等特性构建的。
  • 与 MySQL 类似,TiDB 不允许将主键索引设为不可见。
  • MySQL 中提供的优化器开关 use_invisible_indexes=on 可将所有的不可见索引重新设为可见。该功能在 TiDB 中不可用。

另请参阅