New feature of MySQL8 - index enhancement

1. Hidden index

MySQL 8.0 began to support hidden indexes (invisible index), invisible indexes.

Hidden indexes are not used by the optimizer, but still need to be maintained. Application scenarios: soft deletion, grayscale release.

Soft deletion: We often delete and create indexes online. If it is the previous version, if we delete the index and find out that the deletion is wrong later, I need to create another index. Doing so will greatly affect performance. In MySQL8, we can do this, turning an index into a hidden index (the index is no longer available, and the query optimizer can't use it), and finally we must delete the index before we delete the index.

Gray scale release: It is also similar. We want to conduct some tests online. We can create a hidden index first, which will not affect the current production environment. Then we pass some additional tests and find that this index is no problem, so we can directly put this The index is changed to a formal index to allow the online environment to take effect.

Use case (grayscale release):

create table t1(i int,j int);  --创建一张t1表
create index i_idx on t1(i);  --创建一个正常索引
create index j_idx on t1(j) invisible;  --创建一个隐藏索引

show index from t1\G         --查看索引信息

Use the query optimizer to see:

explain select * from t1 where i=1;
explain select * from t1 where j=1;

Here you can see that the hidden index will not be used.

Here we can open a setting through the switch of the optimizer, which is convenient for us to set the hidden index.

select @@optimizer_switch\G;   --查看 各种参数

The red part is that the default query optimizer is not visible to hidden indexes, and we can modify them through parameters. Make sure we can test with a hidden index.

set session optimizer_switch="use_invisible_indexes=on';   --在会话级别设置查询优化器可以看到隐藏索引

Then use the query optimizer to see:

explain select * from t1 where j=1;

Turn hidden index into visible index (normal index)

alter table t1 alter index j_idx visible;   --变成可见
alter table t1 alter index j_idx invisible;   --变成不可见(隐藏索引)

Finally, the primary key cannot be set to an invisible index (hidden index) (MySQL has restrictions)

2, descending index

MySQL 8.0 began to truly support descending indexes (descendingindex). Only the InnoDB storage engine supports descending indexes, and only BTREE descending indexes are supported. In addition, MySQL 8.0 no longer implicitly sorts GROUP BY operations.

Create a t2 table in MySQL

create table t2(c1 int,c2 int,index idx1(c1 asc,c2 desc));
​
show create table t2\G
 
 

If it is in 5.7, no ascending or descending information is displayed

Let's insert some data to demonstrate the use of descending indexes

insert into t2(c1,c2) values(1,100),(2,200),(3,150),(4,50);

Look at the index usage

explain select * from t2 order by c1,c2 desc;

Let's compare it in 5.7

This shows that an additional sorting operation is required to use the index just now.

Let's change the query

explain select * from t2 order by c1 desc,c2 ;

Used in MySQL8

Another point is that the group by statement is no longer sorted by default after 8

select count(*),c2 from t2 group by c2;

If you want to sort in 8, you need to manually add the sort statement

select count(*),c2 from t2 group by c2 order by c2;

3. Function index

We know before that if a function is added to the query, the index will not take effect, so MySQL8 introduces the function index.

MySQL 8.0.13 began to support the use of function (expression) values ​​in indexes. Support descending index, support index function index of JSON data based on virtual column function.

use function index(expression)

create table t3(c1 varchar(10),c2 varchar(10));
create index idx_c1 on t3(c1);   --普通索引
create index func_idx on t3( (UPPER(c2)) );   --一个大写的函数索引
 
 

show index from t3\G

explain select * from t3 where upper(c1)='ABC' ;  
explain select * from t3 where upper(c2)='ABC' ;

Using Functional Indexes (JSON)

create table t4(data json,index((CAST(data->>'$.name' as char(25)) )));
explain select * from t4 where CAST(data->>'$.name' as char(25)) = 'lijin ';

Functional index is implemented based on virtual column function

The function index is equivalent to adding a new column in MySQL. This column will calculate the result according to your function, and then use the calculated column as the index when using the function index.

Guess you like

Origin blog.csdn.net/m0_70299172/article/details/130496256