create table t1 ( i int, j int, k int, index i_idx (i) invisible ) engine = innodb; create index j_idx on t1 (j) invisible; alter table t1 add index k_idx (k) invisible; 修改已经存在的索引的可见性:
alter table t1 alter index i_idx invisible; alter table t1 alter index i_idx visible; 可以通过information_schema.statistics、show index查看索引的可见性:
>select index_name, is_visible -> from information_schema.statistics -> where table_schema = 'abce' and table_name = 't1'; +------------+------------+ | INDEX_NAME | IS_VISIBLE | +------------+------------+ | i_idx | NO | +------------+------------+ 1 row in set (0.00 sec) >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 | Visible | Expression | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | t1 | 1 | i_idx | 1 | i | A | 0 | NULL | NULL | YES | BTREE | | | NO | NULL | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ 1 row in set (0.00 sec) 不可见索引可以用来测试移除索引后对查询性能的影响。 毕竟对于大表,删除和重建索引是非常昂贵的操作。 系统变量optimizer_switch中的use_invisible_indexes标志控制了优化器是否使用不可见索引来构建执行计划。 如果use_invisible_indexes=off(默认设置),优化器会忽略不可见索引;如果设置为on,索引仍然不可见,但是优化器在生成执行计划的时候会考虑不可见索引。
化器在生成执行计划的时候会考虑不可见索引。
mysql>show variables like '%optimizer_switch%' index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on,use_invisible_indexes=off,skip_scan=on | 返回行数:[1],耗时:14 ms.