MySQL8.0新特性-不可见索引

官方文档: https://dev.MySQL.com/doc/refman/8.0/en/invisible-indexes.html

成都创新互联-专业网站定制、快速模板网站建设、高性价比鱼峰网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式鱼峰网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖鱼峰地区。费用合理售后完善,10多年实体公司更值得信赖。

MySQL 8.0 invisible index学习

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.

MySQL 8.0新特性-不可见索引

举例:

MySQL 8.0新特性-不可见索引

alter table t1 alter index i_idx invisible;

MySQL 8.0新特性-不可见索引

索引设置为不可见,优化器便不考虑此索引,也就是说如果没有该特性,你就得删除该索引和重建该索引来测


网页名称:MySQL8.0新特性-不可见索引
网页URL:http://bzwzjz.com/article/psgcdg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆企业网站建设 网站制作 网站制作公司 手机网站制作设计 重庆外贸网站建设 营销网站建设 成都定制网站建设 响应式网站建设 企业网站建设公司 企业网站建设 成都网站制作 网站制作 成都网站制作 网站设计制作报价 响应式网站设计方案 成都网站制作公司 定制级高端网站建设 成都网站设计 手机网站制作 网站设计 高端定制网站设计 成都企业网站制作