MySQL8.0?索引優(yōu)化invisible?index詳情
前言
MySQL8.0 開始支持不可見索引。 優(yōu)化器根本不使用不可見索引,但會以其他的方式正常維護(hù)。
默認(rèn)情況下 索引是可見的。 通過不可見索引,可以方便數(shù)據(jù)庫管理人員 檢查 索引對查詢性能的影響,而不會進(jìn)行破壞性的更改 。
應(yīng)用場景: 軟刪除,灰度發(fā)布
-- 創(chuàng)建測試表
mysql> create table t1(i int ,j int);
Query OK, 0 rows affected (0.03 sec)
-- 創(chuàng)建索引
mysql>
mysql> create index idx_i on t1(i);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> -- 創(chuàng)建 一個隱藏索引
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 分別創(chuàng)建了索引, 其中j 為不可見索引 ,即默認(rèn)優(yōu)化器不會使用到該索引的。
索引使用情況測試
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)如何臨時讓優(yōu)化器可以看到這個索引呢?
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)
-- 設(shè)置開關(guān)
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)
-- 此時可以看到 優(yōu)化器已經(jīng)可以看到這個索引啦
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
注意: 主鍵索引 是不能設(shè)置 不可見的 。
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 是必須可見的, 不能設(shè)置為不可見索引。
在生產(chǎn)環(huán)境中 也可以利用隱藏索引進(jìn)行 SQL 語句的性能測試,或者對索引進(jìn)行邏輯刪除,索引的灰度發(fā)布測試,以及軟刪除索引等。
到此這篇關(guān)于MySQL8.0 索引優(yōu)化invisible index詳情的文章就介紹到這了,更多相關(guān)MySQL索引優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探索MySQL?8中utf8mb4釋放多語言數(shù)據(jù)的強(qiáng)大潛力
這篇文章主要為大家介紹了探索MySQL?8中utf8mb4釋放多語言數(shù)據(jù)的強(qiáng)大潛力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
windows下mysql中binlog日志分析和數(shù)據(jù)恢復(fù)問題
這篇文章主要介紹了windows下mysql中binlog日志分析和數(shù)據(jù)恢復(fù)問題,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
mysql從5.7平滑升級到8.0.27的實(shí)現(xiàn)
mysql8.0已經(jīng)到了穩(wěn)定期,本文主要介紹了mysql從5.7平滑升級到8.0.27的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06
deepin 2014系統(tǒng)下安裝mysql數(shù)據(jù)庫的方法步驟
這篇文章主要給大家介紹了在deepin 2014系統(tǒng)下安裝mysql數(shù)據(jù)庫的方法步驟,文中通過圖文介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
mysql 轉(zhuǎn)換NULL數(shù)據(jù)方法(必看)
下面小編就為大家?guī)硪黄猰ysql 轉(zhuǎn)換NULL數(shù)據(jù)方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
MySQL千萬級數(shù)據(jù)表的優(yōu)化實(shí)戰(zhàn)記錄
千萬級大表如何優(yōu)化,這是一個很有技術(shù)含量的問題,通常我們的直覺思維都會跳轉(zhuǎn)到拆分或者數(shù)據(jù)分區(qū),這篇文章主要給大家介紹了關(guān)于MySQL千萬級數(shù)據(jù)表優(yōu)化的相關(guān)資料,需要的朋友可以參考下2021-08-08

