加入收藏 | 设为首页 | 会员中心 | 我要投稿 临夏站长网 (https://www.0930zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL 8.0新特性-不可见索引

发布时间:2022-04-06 10:59:37 所属栏目:MySql教程 来源:互联网
导读:MySQL支持不可见索引,即优化器不会使用该索引。 不可见索引特性不可以用于主键。 默认索引是可见的。可以在create table、create index、alter table操作中使用关键字visible、invisible来指定索引是否可见。 create table t1 ( i int, j int, k int, ind
        MySQL支持不可见索引,即优化器不会使用该索引。 不可见索引特性不可以用于主键。
 
       默认索引是可见的。可以在create table、create index、alter table操作中使用关键字visible、invisible来指定索引是否可见。
 
       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.
 
举例:
 
alter table t1 alter index i_idx invisible;
 
索引设置为不可见,优化器便不考虑此索引,也就是说如果没有该特性,你就得删除该索引和重建该索引来测.

(编辑:临夏站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!