MySQL8.0 索引优化invisible index详情
作者:阿常呓语 时间:2024-01-21 11:35:40
前言
MySQL8.0 开始支持不可见索引。 优化器根本不使用不可见索引,但会以其他的方式正常维护。
默认情况下 索引是可见的。 通过不可见索引,可以方便数据库管理人员 检查 索引对查询性能的影响,而不会进行破坏性的更改 。
应用场景: 软删除,灰度发布
-- 创建测试表
mysql> create table t1(i int ,j int);
Query OK, 0 rows affected (0.03 sec)
-- 创建索引
mysql>
mysql> create index idx_i on t1(i);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> -- 创建 一个隐藏索引
mysql> create index idx_j on t1(j) invisible ;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
-- 查看索引
mysql> show index from t1\G
*************************** 1. row ***************************
Table: t1
Non_unique: 1
Key_name: idx_i
Seq_in_index: 1
Column_name: i
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
Visible: YES
Expression: NULL
*************************** 2. row ***************************
Table: t1
Non_unique: 1
Key_name: idx_j
Seq_in_index: 1
Column_name: j
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
Visible: NO
Expression: NULL
2 rows in set (0.00 sec)
上面 对 i
,j
分别创建了索引, 其中j
为不可见索引 ,即默认优化器不会使用到该索引的。
索引使用情况测试
mysql> explain select * from t1 where i= 10\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: ref
possible_keys: idx_i
key: idx_i
key_len: 5
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
mysql> explain select * from t1 where j= 10\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
如何临时让优化器可以看到这个索引呢?
mysql> select @@optimizer_switch\G
*************************** 1. row ***************************
@@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 row in set (0.00 sec)
-- 设置开关
mysql> set session optimizer_switch='use_invisible_indexes=on';
Query OK, 0 rows affected (0.00 sec)
mysql> select @@optimizer_switch\G
*************************** 1. row ***************************
@@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=on,skip_scan=on
1 row in set (0.00 sec)
-- 此时可以看到 优化器已经可以看到这个索引啦
mysql> explain select * from t1 where j= 10\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: ref
possible_keys: idx_j
key: idx_j
key_len: 5
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
修改索引的可见性
mysql> ALTER TABLE t1 ALTER INDEX idx_j INVISIBLE;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE t1 ALTER INDEX idx_j VISIBLE;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
注意: 主键索引 是不能设置 不可见的 。
create table t2(`id` int not null auto_increment, primary key (`id`));
mysql> show index from t2\G
*************************** 1. row ***************************
Table: t2
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
Visible: YES
Expression: NULL
1 row in set (0.01 sec)
mysql> alter table t2 alter index PRIMARY key(`id`) invisible;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PRIMARY invisible' at line 1
测试 添加 主键 为不可见索引:
mysql> create table t3(id int not null);
Query OK, 0 rows affected (0.03 sec)
mysql> alter table t3 add primary key pk_t3(id) invisible;
ERROR 3522 (HY000): A primary key index cannot be invisible
primary key
是必须可见的, 不能设置为不可见索引。
在生产环境中 也可以利用隐藏索引进行 SQL 语句的性能测试,或者对索引进行逻辑删除,索引的灰度发布测试,以及软删除索引等。
来源:https://blog.csdn.net/u010339879/article/details/127043431
标签:MySQL,索引,优化,invisible,index
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用FrongPage设计网页花样
2008-09-17 10:47:00
详解用pyecharts Geo实现动态数据热力图城市找不到问题解决
2022-02-04 14:10:29
![](https://img.aspxhome.com/file/2023/2/107632_0s.png)
python自定义时钟类、定时任务类
2021-02-12 12:21:37
pandas分区间,算频率的实例
2021-12-12 01:27:24
![](https://img.aspxhome.com/file/2023/9/107479_0s.jpg)
SQL Server子查询的深入理解
2024-01-15 14:09:46
![](https://img.aspxhome.com/file/2023/9/89649_0s.png)
[译]Javascript风格要素(一)
2008-02-28 12:58:00
SQL实现LeetCode(180.连续的数字)
2024-01-24 13:45:21
Vue 实例中使用$refs的注意事项
2024-05-02 16:36:01
![](https://img.aspxhome.com/file/2023/5/130185_0s.jpg)
Python基于TensorFlow接口实现深度学习神经网络回归
2022-07-17 22:38:28
asp去除html的函数代码分析附实例说明
2023-07-02 20:09:18
神经网络(BP)算法Python实现及应用
2021-11-12 20:00:14
![](https://img.aspxhome.com/file/2023/9/98319_0s.png)
Microsoft Enterprise Library 5.0 如何集成MyS
2011-03-16 15:19:00
python线程信号量semaphore使用解析
2023-02-08 08:47:24
利用python实现冒泡排序算法实例代码
2021-06-29 17:00:14
![](https://img.aspxhome.com/file/2023/2/90522_0s.png)
js简单实现Select互换数据的方法
2023-07-20 00:50:46
![](https://img.aspxhome.com/file/2023/5/55965_0s.jpg)
Mac PyCharm中的.gitignore 安装设置教程
2023-02-23 16:48:14
![](https://img.aspxhome.com/file/2023/2/67342_0s.jpg)
Anaconda安装OpenCV的方法图文教程
2021-11-30 18:22:09
![](https://img.aspxhome.com/file/2023/7/115147_0s.png)
python not关键字实例用法
2023-12-05 10:25:50
网页设计布局原则
2010-04-20 17:18:00
SQL Server数据库开发的二十一条法则
2024-01-18 05:30:40