Word MySQL 8.0 has been released for four years since its first version. Version 8.0 has made considerable improvements and reconstructions in terms of functions and code. After communicating with friends in the DBA circle, most of them are still using versions 5.6 and 5.7, and a small number of them have adopted MySQL 8.0. In order to keep up with the pace of database development and enjoy the benefits of technology as soon as possible, we plan to introduce MySQL 8.0 into Youzan's database system. Before implementation, we will conduct a series of studies and tests on the new features and functions, configuration parameters, upgrade methods, compatibility, etc. of MySQL 8.0. More articles will be published in the future. This article is the first one to learn about the new features of MySQL 8.0. Let’s talk about invisible indexes. Invisible Index The invisible index refers to the optimizer. When analyzing the execution plan (by default), the optimizer will ignore the index with the invisible attribute set.
Without further ado, let's test a few examples How to set invisible index We can set the visibility of the index by using create table, create index, and alter table with the keywords VISIBLE|INVISIBLE. 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) The role of invisible indexes Faced with a large number of historical indexes, after several rounds of development and replacement of old and new developers, DBAs probably dare not delete the indexes directly, especially when encountering large tables larger than 100G. Directly deleting the index will increase the risk of database stability. With the invisible index feature, the DBA can set the index to be invisible while observing the slow query records and thread running status of the database. If the database has no related slow queries for a long time and thread_running is relatively stable, the index can be taken offline. Otherwise, you can quickly set the index to be visible and restore business access.
After setting the invisible index, the execution plan cannot use the index 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) Notes on using invisible indexes
Invisible indexes are for non-primary key indexes. The primary key cannot be set to invisible. The primary key here includes explicit primary keys or implicit primary keys (when there is no primary key, it is promoted to the unique index of the primary key). We can use the following example to illustrate this rule. 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) ### In the absence of a primary key, the unique key is treated as an implicit primary key and cannot be set to invisible. 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) Invisible indexes cannot be accessed, otherwise an error will be reported. mysql> select * from t2 force index(j_idx) where j=3; ERROR 1176 (42000): Key 'j_idx' doesn't exist in table 't2' Setting an index to be invisible requires acquiring an MDL lock, which can cause database jitter when encountering long transactions The unique index is set to be invisible, which does not mean that the uniqueness constraint of the index itself is invalid. 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' summary Actually there is nothing much to say, I wish you all a happy use. -The End- The above is a detailed explanation of the invisible index in MySQL 8.0. For more information about the invisible index in MySQL 8.0, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to deploy Vue project using Docker image + nginx
>>: React Hooks Detailed Explanation
In the Linux system, in addition to various accou...
Table of contents Parent component communicates w...
There are two common ways to download files in da...
Table of contents vue router 1. Understand the co...
1. Command Introduction The chkconfig command is ...
Preparation 1. Start the virtual machine 2. git t...
This article shares the specific code of JQuery t...
As the number of visits increases, for some time-...
Summarize This article ends here. I hope it can b...
This article shares the specific code of JavaScri...
Recently, several data anomalies have occurred in...
XHTML is the standard website design language cur...
Preface This article mainly introduces the releva...
The Core Asset Management Project requires el-tra...
1. Basic lines 2. Special effects (the effects ar...