国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

詳解MySQL 8.0 之不可見(jiàn)索引

瀏覽:5日期:2023-10-10 10:14:39

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

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

不可見(jiàn)索引

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

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

話不多說(shuō),我們先測(cè)試幾個(gè)例子

如何設(shè)置不可見(jiàn)索引

我們可以通過(guò)帶上關(guān)鍵字VISIBLE|INVISIBLE的create table,create index,alter table 設(shè)置索引的可見(jiàn)性。

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: 0mysql> alter table t1 add index k_idx (k) invisible;Query OK, 0 rows affected (0.10 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> 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: 0mysql> 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)

不可見(jiàn)索引的作用

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

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

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

設(shè)置完不可見(jiàn)索引,執(zhí)行計(jì)劃無(wú)法使用索引

mysql> show create table t2 G*************************** 1. row *************************** Table: t2Create 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_ci1 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: 0mysql> explain select * from t2 where j=3G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t2 partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 7 filtered: 14.29 Extra: Using where1 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: 0mysql> explain select * from t2 where j=3G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t2 partitions: NULL type: constpossible_keys: j_idx key: j_idx key_len: 4 ref: const rows: 1 filtered: 100.00 Extra: Using index1 row in set, 1 warning (0.01 sec)

使用不可見(jiàn)索引的注意事項(xiàng)

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

不可見(jiàn)索引是針對(duì)非主鍵索引的。主鍵不能設(shè)置為不可見(jiàn),這里的 主鍵 包括顯式的主鍵或者隱式主鍵(不存在主鍵時(shí),被提升為主鍵的唯一索引) ,我們可以用下面的例子展示該規(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)### 沒(méi)有主鍵的情況下,唯一鍵被當(dāng)做隱式主鍵,不能設(shè)置 不可見(jiàn)。mysql> alter table t2 alter index j_idx invisible;ERROR 3522 (HY000): A primary key index cannot be invisiblemysql>mysql> alter table t2 add primary key (i);Query OK, 0 rows affected (0.44 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> 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: 0mysql> 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) 不能訪問(wèn)不可見(jiàn)索引,否則報(bào)錯(cuò)。

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

設(shè)置索引為不可見(jiàn)需要獲取MDL鎖,遇到長(zhǎng)事務(wù)會(huì)引發(fā)數(shù)據(jù)庫(kù)抖動(dòng)

唯一索引被設(shè)置為不可見(jiàn),不代表索引本身唯一性的約束失效

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’

小結(jié)

其實(shí)沒(méi)啥說(shuō)的,祝大家用的愉快。

-The End-

以上就是詳解MySQL 8.0 之不可見(jiàn)索引的詳細(xì)內(nèi)容,更多關(guān)于MySQL 8.0 不可見(jiàn)索引的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 午夜日韩视频 | www亚洲成人| 亚洲国产品综合人成综合网站 | 国产精品永久免费自在线观看 | 国产精品国产自线在线观看 | 国产精成人品 | 99视频精品免费99在线 | 国产一级生活片 | 黄色欧美网站 | 国产一区二区三区四区波多野结衣 | 欧美大片aaaa一级毛片 | 亚洲免费视频在线观看 | 国产亚洲精品久久久久久无 | 三级毛片网站 | 毛片免费观看的视频在线 | 欧美高清videossex19 | 国产精品九九免费视频 | 成年人网站免费视频 | 久久久亚洲欧美综合 | 美女视频免费黄色 | 成人免费大片黄在线观看com | 最新版天堂资源中文官网 | 狠狠色丁香久久婷婷综 | 99久久免费精品视频 | 性感美女视频免费网站午夜 | 精品欧美一区二区三区精品久久 | 一级一级毛片看看 | 美女张开腿黄网站免费 | 久久狠狠色狠狠色综合 | 黄色a∨| 长腿校花被啪到腿软视频 | 免费在线观看黄色毛片 | 日韩中文字幕网站 | 窝窝女人体国产午夜视频 | 中文字幕一区视频一线 | 九九视频在线观看视频6 | 在线一区国产 | 中文字幕日韩一区二区不卡 | 国产日韩精品欧美一区 | 国产成人v视频在线观看 | 亚洲欧美在线一区二区 |