濮阳杆衣贸易有限公司

主頁 > 知識庫 > 詳解MySQL 8.0 之不可見索引

詳解MySQL 8.0 之不可見索引

熱門標簽:芒果電話機器人自動化 廣東人工電話機器人 申請外呼電話線路 百度地圖圖標標注中心 湖南人工外呼系統(tǒng)多少錢 南通自動外呼系統(tǒng)軟件 日照旅游地圖標注 石家莊電商外呼系統(tǒng) 信陽穩(wěn)定外呼系統(tǒng)運營商

MySQL 8.0 從第一版release 到現在已經走過了4個年頭了,8.0版本在功能和代碼上做了相當大的改進和重構。和DBA圈子里的朋友交流,大部分還是5.6 ,5.7的版本,少量的走的比較靠前采用了MySQL 8.0。為了緊追數據庫發(fā)展的步伐,能夠盡早享受技術紅利,我們準備將MySQL 8.0引入到有贊的數據庫體系。

落地之前 我們會對MySQL 8.0的新特性和功能,配置參數,升級方式,兼容性等等做一系列的學習和測試。以后陸陸續(xù)續(xù)會發(fā)布文章出來。本文算是MySQL 8.0新特性學習的第一篇吧,聊聊 不可見索引。

不可見索引

不可見索引中的不可見是針對優(yōu)化器而言的,優(yōu)化器在做執(zhí)行計劃分析的時候(默認情況下)是會忽略設置了不可見屬性的索引。

為什么是默認情況下,如果 optimizer_switch設置use_invisible_indexes=ON 是可以繼續(xù)使用不可見索引。

話不多說,我們先測試幾個例子

如何設置不可見索引

我們可以通過帶上關鍵字VISIBLE|INVISIBLE的create table,create index,alter table 設置索引的可見性。

mysql> create table t1 (i int,
   > j int,
   > k int,
   > index i_idx (i) invisible) engine=innodb;
Query OK, 0 rows affected (0.41 sec)

mysql> create index j_idx on t1 (j) invisible;
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table t1 add index k_idx (k) invisible;
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select index_name,is_visible from information_schema.statistics where table_schema='test' and table_name='t1';
+------------+------------+
| INDEX_NAME | IS_VISIBLE |
+------------+------------+
| i_idx   | NO     |
| j_idx   | NO     |
| k_idx   | NO     |
+------------+------------+
3 rows in set (0.01 sec)

mysql> alter table t1 alter index i_idx visible;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select index_name,is_visible from information_schema.statistics where table_schema='test' and table_name='t1';
+------------+------------+
| INDEX_NAME | IS_VISIBLE |
+------------+------------+
| i_idx   | YES    |
| j_idx   | NO     |
| k_idx   | NO     |
+------------+------------+
3 rows in set (0.00 sec)

不可見索引的作用

面對歷史遺留的一大堆索引,經過數輪新老交替開發(fā)和DBA估計都不敢直接將索引刪除,尤其是遇到比如大于100G的大表,直接刪除索引會提升數據庫的穩(wěn)定性風險。

有了不可見索引的特性,DBA可以一邊設置索引為不可見,一邊觀察數據庫的慢查詢記錄和thread running 狀態(tài)。如果數據庫長時間沒有相關慢查詢 ,thread_running比較穩(wěn)定,就可以下線該索引。反之,則可以迅速將索引設置為可見,恢復業(yè)務訪問。

Invisible Indexes 是 server 層的特性,和引擎無關,因此所有引擎(InnoDB, TokuDB, MyISAM, etc.)都可以使用。

設置完不可見索引,執(zhí)行計劃無法使用索引

mysql> show create table t2 \G
*************************** 1. row ***************************
    Table: t2
Create Table: CREATE TABLE `t2` (
 `i` int NOT NULL AUTO_INCREMENT,
 `j` int NOT NULL,
 PRIMARY KEY (`i`),
 UNIQUE KEY `j_idx` (`j`) /*!80000 INVISIBLE */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
mysql> insert into t2(j) values(1),(2),(3),(4),(5),(6),(7);
Query OK, 7 rows affected (0.04 sec)
Records: 7 Duplicates: 0 Warnings: 0


mysql> explain select * from t2 where j=3\G
*************************** 1. row ***************************
      id: 1
 select_type: SIMPLE
    table: t2
  partitions: NULL
     type: ALL
possible_keys: NULL
     key: NULL
   key_len: NULL
     ref: NULL
     rows: 7
   filtered: 14.29
    Extra: Using where
1 row in set, 1 warning (0.01 sec)

mysql> alter table t2 alter index j_idx visible;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select * from t2 where j=3\G
*************************** 1. row ***************************
      id: 1
 select_type: SIMPLE
    table: t2
  partitions: NULL
     type: const
possible_keys: j_idx
     key: j_idx
   key_len: 4
     ref: const
     rows: 1
   filtered: 100.00
    Extra: Using index
1 row in set, 1 warning (0.01 sec)

使用不可見索引的注意事項

The feature applies to indexes other than primary keys (either explicit or implicit).

不可見索引是針對非主鍵索引的。主鍵不能設置為不可見,這里的 主鍵 包括顯式的主鍵或者隱式主鍵(不存在主鍵時,被提升為主鍵的唯一索引) ,我們可以用下面的例子展示該規(guī)則。

mysql> create table t2 (
   >i int not null,
   >j int not null ,
   >unique j_idx (j)
   >) ENGINE = InnoDB;
Query OK, 0 rows affected (0.16 sec)

mysql> select index_name,is_visible from information_schema.statistics where table_schema='test' and table_name='t2';
+------------+------------+
| INDEX_NAME | IS_VISIBLE |
+------------+------------+
| j_idx   | YES    |
+------------+------------+
1 row in set (0.00 sec)

### 沒有主鍵的情況下,唯一鍵被當做隱式主鍵,不能設置 不可見。
mysql> alter table t2 alter index j_idx invisible;
ERROR 3522 (HY000): A primary key index cannot be invisible
mysql>
mysql> alter table t2 add primary key (i);
Query OK, 0 rows affected (0.44 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select index_name,is_visible from information_schema.statistics where table_schema='test' and table_name='t2';
+------------+------------+
| INDEX_NAME | IS_VISIBLE |
+------------+------------+
| j_idx   | YES    |
| PRIMARY  | YES    |
+------------+------------+
2 rows in set (0.01 sec)

mysql> alter table t2 alter index j_idx invisible;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select index_name,is_visible from information_schema.statistics where table_schema='test' and table_name='t2';
+------------+------------+
| INDEX_NAME | IS_VISIBLE |
+------------+------------+
| j_idx   | NO     |
| PRIMARY  | YES    |
+------------+------------+
2 rows in set (0.01 sec)

force /ignore index(index_name) 不能訪問不可見索引,否則報錯。

mysql> select * from t2 force index(j_idx) where j=3;
ERROR 1176 (42000): Key 'j_idx' doesn't exist in table 't2'

設置索引為不可見需要獲取MDL鎖,遇到長事務會引發(fā)數據庫抖動

唯一索引被設置為不可見,不代表索引本身唯一性的約束失效

mysql> select * from t2;
+---+----+
| i | j |
+---+----+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 11 |
+---+----+
8 rows in set (0.00 sec)
mysql> insert into t2(j) values(11);
ERROR 1062 (23000): Duplicate entry '11' for key 't2.j_idx'

小結

其實沒啥說的,祝大家用的愉快。

-The End-

以上就是詳解MySQL 8.0 之不可見索引的詳細內容,更多關于MySQL 8.0 不可見索引的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • MySQL全文索引、聯合索引、like查詢、json查詢速度哪個快
  • MySQL全文索引實現簡單版搜索引擎實例代碼
  • MySQL創(chuàng)建全文索引分享
  • MySQL全文索引應用簡明教程
  • 基于mysql全文索引的深入理解
  • MySQL索引失效的幾種情況詳析
  • Mysql普通索引與唯一索引的選擇詳析
  • 淺析MysQL B-Tree 索引
  • MySQL8.0中的降序索引
  • MySQL 8.0 之索引跳躍掃描(Index Skip Scan)
  • Mysql索引常見問題匯總
  • MySql索引提高查詢速度常用方法代碼示例
  • MySQL 全文索引的原理與缺陷

標簽:公主嶺 合肥 牡丹江 阿里 沈陽 天津 呼和浩特 惠州

巨人網絡通訊聲明:本文標題《詳解MySQL 8.0 之不可見索引》,本文關鍵詞  詳解,MySQL,8.0,之不,可見,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解MySQL 8.0 之不可見索引》相關的同類信息!
  • 本頁收集關于詳解MySQL 8.0 之不可見索引的相關信息資訊供網民參考!
  • 推薦文章
    黑山县| 朝阳县| 稻城县| 慈利县| 云梦县| 寿光市| 安吉县| 峨边| 沈丘县| 甘洛县| 定远县| 乌鲁木齐市| 合江县| 闽侯县| 宜川县| 凤阳县| 宣汉县| 佛教| 磴口县| 沂南县| 桂林市| 策勒县| 友谊县| 玉环县| 白沙| 霍城县| 华池县| 大方县| 揭西县| 杂多县| 镇雄县| 大荔县| 镇沅| 扎囊县| 太保市| 仪陇县| 湖北省| 大安市| 溧水县| 荣昌县| 石楼县|