温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL8.0中invisible index特点及作用

发布时间:2020-05-28 11:27:55 来源:网络 阅读:259 作者:三月 栏目:MySQL数据库

不知道大家之前对类似MySQL8.0中invisible index特点及作用的文章有无了解,今天我在这里给大家再简单的讲讲。感兴趣的话就一起来看看正文部分吧,相信看完MySQL8.0中invisible index特点及作用你一定会有所收获的。


invisible index特点

默认创建的索引都是visible,如果需要invisible索引创建的时候需要指定invisible参数。

默认无法使用invisible index索引,需要开启参数optimizer_switch='use_invisible_indexes=on'才能使用;

使用force index也无法使用,会报错;

索引的visible和invisible属性可以相互转换,即使表很大,这个过程也是很快的;

primary key不能改成invisible;


invisible index作用

在生产环境中,往往一个表是非常大的,我们想要测试一条SQL不使用某个索引的执行效率,如果直接删除这个索引,可能代价比较大,但是把索引改成不可见模式,再去测试,时间是很快的。

mysql> create index idx_emp_no on t_group(emp_no) invisible;
Query OK, 0 rows affected (0.22 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from t_group;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| t_group |          1 | idx_emp_no |            1 | emp_no      | A         |          10 |     NULL |   NULL |      | BTREE      |         |               | NO      | NULL       |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.08 sec)

mysql> desc select * from t_group where emp_no=31112;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t_group | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |    10.00 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> desc select * from t_group force index(idx_emp_no) where emp_no=31112;
ERROR 1176 (42000): Key 'idx_emp_no' doesn't exist in table 't_group'

mysql> desc select /*+ set_var(optimizer_switch='use_invisible_indexes=on') */ * from t_group where emp_no=31112;
+----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | t_group | NULL       | ref  | idx_emp_no    | idx_emp_no | 4       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

看完MySQL8.0中invisible index特点及作用这篇文章,大家觉得怎么样?如果想要了解更多相关,可以继续关注我们的行业资讯板块。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI