当前位置:网站首页>Hidden index and descending index in MySQL 8.0 (new feature)
Hidden index and descending index in MySQL 8.0 (new feature)
2022-07-27 01:00:00 【PeakXYH】
MySQL8.0 Hidden index in
Overview of hidden indexes
Hiding the index means hiding the index , Query in progress 、 When sorting and other operations, the system will directly ignore the index , Hidden index can be understood as soft deletion of index , That is, the index is set to invalid , But in fact, the corresponding index is not deleted , It avoids the accidental deletion of the index , The system reconstruction index needs to consume a lot of time and system resources , Set the hidden index to be visible when necessary , The index returns to its original function
Why should there be hidden indexes ?
In short : It avoids the accidental deletion of the index , The system reconstruction index needs to consume a lot of time and system resources ( You can deeply experience it through the following two specific situations ), stay MySQL5.7 You can only rebuild the index , stay MySQL8.0 You can use the hidden index to solve
Scenario analysis 1:
When we delete an index , However, it was found that the performance was seriously affected or some errors occurred after deletion , Then we need to rebuild the index , But in the case of a large amount of data , Rebuilding the index is very time-consuming , So if you can make the index temporarily invalid without deleting the index , Then when an error is found, you don't need to rebuild the index, but restore the index to be valid .
Scenario analysis 2:
When we don't know whether our index is valid , Then delete the index , Whether the index is invalid can be judged by comparing whether there is a difference between the before and after operations . If there is no difference , Then deletion is correct ( Because maintaining indexes requires resources ), If you find that deleting the index seriously affects the query performance , Then you need to rebuild the deleted index . But delete the index 、 Rebuilding the index requires system resources , So if we can make the index temporarily invalid , Then the above problems can be avoided .
Hide the usage scenario of the index
- When the index needs to be deleted , First, it can be set to hide the index , At this time, the index is invalid , Then you can test , If it is set to hide the index, it has no effect , Then you can use hard deletion , Otherwise, it can be set as non hidden .
- If you want to verify the impact of query performance after an index is deleted , You can temporarily set the index to hidden .
Considerations for hiding indexes
- The primary key cannot be set to hide the index
- When the index is set to hidden, the index is unavailable ( invalid ), But its content will still be updated
- Hidden indexes are invisible to the optimizer , But it can also be visible to the optimizer through parameter settings
- because InnoDB The storage engine is built with primary keys B+ Trees store data , Therefore, the primary key cannot be set to invalid , Otherwise, the storage engine will reselect a field as the primary key , Rebuild the entire table B+ Trees store data
- Although the index is set to hidden , But its content is still updated , It's just that indexes won't be used when querying and other operations , So if an index is to be hidden for a long time , Then you'd better delete it , Because it will affect the update of data 、 Efficiency of operations such as deletion
Hidden use code demonstration
- Set hidden indexes when creating tables
CREATE TABLE xiaoyuanhao(
id INT,
username INT,
PASSWORD INT,
INDEX password_index(PASSWORD) invisible
)
-- By default visible, If you are using invisible Then it's invisible
- Set the index in the table to hidden
ALTER TABLE xiaoyuanhao ALTER INDEX password_index invisible;
MySQL8.0 Descending index in
Overview of descending index
stay MySQL4.0 Although it supports the creation of descending indexes in syntax , however InnoDB The underlying implementation is still the architecture of indexing in ascending order ( Both ascending and descending indexes are built in ascending order ), In the specific implementation, it is realized by reverse scanning , But this will greatly reduce the efficiency of the database , stay MySQL8.0 Supports the construction of descending index , That is, the underlying index structure is arranged in descending order of index columns
Use and test of descending index
- Create an ascending index , Query in descending order
CREATE TABLE xiaoyuanhao (id INT,PASSWORD INT,username VARCHAR(20));
-- Create a table
CREATE INDEX username_index ON xiaoyuanhao(username);
-- In ascending order username Create index
EXPLAIN SELECT username FROM xiaoyuanhao ORDER BY username DESC;
-- Query in descending order

It can be seen that there is a reverse scan , inefficiency
- Create a descending index , Query in descending order at the same time
DROP INDEX username_index ON xiaoyuanhao;
-- Delete the previous ascending order used
CREATE INDEX username_index ON xiaoyuanhao(username DESC);
-- Create a descending index
EXPLAIN SELECT username FROM xiaoyuanhao ORDER BY username DESC;
-- Query in descending order

It can be seen here that the index is used directly , No reverse scanning , It can be seen that Mysql8.0 The bottom layer of supports the creation of descending indexes , It improves the efficiency of descending query
边栏推荐
猜你喜欢
![[ciscn2019 North China Day1 web5] cyberpunk](/img/84/b186adc8becfc9b3def7dfd8e4cd41.png)
[ciscn2019 North China Day1 web5] cyberpunk

Flink 1.15 implements SQL script to recover data from savepointh
![[ciscn2019 southeast China division]double secret](/img/51/9597968ff1747a67e10a70b785ee9f.png)
[ciscn2019 southeast China division]double secret

One of the Flink requirements - sideoutput (Application of side output flow: output the temperature higher than 30 ℃ to the mainstream, and output the temperature lower than 30 ℃ to the side flow)
![[ciscn2019 finals Day2 web1]easyweb](/img/36/1ca4b6cae4e0dda0916b511d4bcd9f.png)
[ciscn2019 finals Day2 web1]easyweb

Rational selection of (Spark Tuning ~) operator

Flink中的状态管理
![[红明谷CTF 2021]write_shell](/img/f5/c3a771ab7b40311e37a056defcbd78.png)
[红明谷CTF 2021]write_shell

10个Web API

JSCORE day_ 05(7.6)
随机推荐
[By Pass] WAF 的绕过方式
Detailed explanation of this point in JS
Status management in Flink
07 - 日志服务器的搭建与攻击
Spark累加器(Accumulator)
Ah ah ah ah ah a
数据仓库知识点
[SQL注入] 报错注入
Leetcode 302 weekly games
JSCORE day_01(6.30) RegExp 、 Function
[BJDCTF2020]EzPHP
重学JSON.stringify
2022.7.13
CUDA version difference between NVIDIA SMI and nvcc -v
[CISCN2019 华北赛区 Day1 Web5]CyberPunk
Golang切片make与new的区别
JSCORE day_ 01(6.30) RegExp 、 Function
[BJDCTF2020]EzPHP
VMware Workstation 虚拟机启动就直接蓝屏重启问题解决
基于Flink实时项目:用户行为分析(三:网站总浏览量统计(PV))