The Ali interviewer asked me to talk: the bottom-level implementation of descending index in Mysql, simple!

What is a descending index

You may be familiar with the index, but relatively new to the descending index. In fact, the descending index is a subset of the index.

We usually use the following statement to create an index:

create index idx_t1_bcd on t1(b,c,d);

The above sql means that in the t1 table, a joint index is created for the three fields of b, c, and d.

But what you don't know is that the above SQL is actually equivalent to the following SQL:

create index idx_t1_bcd on t1(b asc,c asc,d asc);

ascIt means ascending order, and the index created using this syntax is called ascending index . That is, when we usually create indexes, we create ascending indexes.

You might think that when creating an index, you can set asc for the field. Can you also set desc?

Of course it is possible, such as the following three sentences:

create index idx_t1_bcd on t1(b desc,c desc,d desc);
create index idx_t1_bcd on t1(b asc,c desc,d desc);
create index idx_t1_bcd on t1(b asc,c asc,d desc);

This syntax is also supported in mysql, and the index created using this syntax is called a descending index .

The key problem is: before Mysql8.0, it was only support at the syntax level, and there was no real support at the bottom .

We use Mysql7 and Mysql8 respectively to illustrate with examples:

Create a table in Mysql7 and Mysql8 respectively, with five fields a, b, c, d, e:

create table t1 (
a int primary key,
b int,
c int,
d int,
e varchar(20)
) engine=InnoDB;

Then create a descending index:

create index idx_t1_bcd on t1(b desc,c desc,d desc);

After the creation is successful, we use the following SQL to check the index information:

show index from t1;

In Mysql7 you will get the result:
Insert picture description here

In Mysql8, you will get the result:
Insert picture description here
We only care about the three rows of records whose Key_name is idx_t1_bcd. You should be careful to find that the results of the Collation field in these two results are different:

• In Mysql7, the results of the Collation field are A, A, A, indicating that the sorting method of the three fields b, c, and d is asc

• In Mysql8, the results of the Collation field are D, D, D, indicating that the sorting method of the three fields b, c, and d is desc

But when we created the index, we clearly specified the sorting method of the three fields b, c, and d at the grammatical level as desc. This shows that the descending index in Mysql7 is only supported at the grammatical level, and the bottom layer does not really support it. , And fixed ascending index. In Mysql8, descending index is really supported from the bottom.

So far, you should have a general understanding of ascending index and descending index, but you don't really understand it, because you don't know how the bottom layer of ascending index and descending index is
implemented.

We know that indexes are used to improve query speed, but why can indexes improve query speed?

Given you a sequence of numbers, such as [1,3,7,9,2,5,4,6,8], this is an unordered sequence or array, now if you want to improve the query speed of this sequence, you will first doing what?

I believe most people can think of sorting first. First, sort this unordered sequence from smallest to largest. For example, get [1,2,3,4,5,6,7,8,9], With this ordered sequence, we can use algorithms such as dichotomy to improve the query speed of this sequence.

What I want to tell you with this example is: If you want to improve the query speed of the data collection, you can sort the data first.

Therefore, the data stored in the Mysql table is the same. If we want to improve the query speed of this table, we can sort the data in this table first, then a row of data in the table includes many fields. Want to sort these data rows, which fields should we use to determine this order? This is the index. The column you specify when creating the index is used to sort the data rows in the table.

For example, we still use the t1 table created above to insert 8 pieces of data into the t1 table:

insert into t1 values(4,3,1,1,'d');
insert into t1 values(1,1,1,1,'a');
insert into t1 values(8,8,8,8,'h');
insert into t1 values(2,2,2,2,'b');
insert into t1 values(5,2,3,5,'e');
insert into t1 values(3,3,2,2,'c');
insert into t1 values(7,4,5,5,'g');
insert into t1 values(6,6,4,4,'f');

Then the data must be stored in the file, so the format of saving the data in the file is roughly as follows, and the order is consistent with the insertion order:

4311d
1111a
8888h
2222b
5235e
3322c
7455g
6644f

Note that t1 is the storage engine of Innodb, and the a field is the primary key, so when the Innodb storage engine processes these inserted data, it will sort by the primary key, that is, the format of saving these data in the file I mentioned above is not accurate .

And if we look up data based on the above storage method, such as looking for the row of a=3, the search needs to start from the first row of records, then it will be searched 6 times, and if we follow the above data according to a Sort by field size:

1111a
2222b
3322c
4311d
5235e
6644f
7455g
8888h

After sorting, if we still find the row of a=3, we only need to check it 3 times. And there is another advantage of this is that if we now need to find the row of data a=3.5, if we based on the storage method before unsorted, we need to query all 8 rows of data to finally determine that the row of data a=3.5 does not exist, and if We use the storage method after sorting, we only need to check 4 times, because when you check 4311dthis row of records, you will find that 4>3.5, you can already confirm that the row of a=3.5 is not Existed.

And if we create an index on t1 now, just like the index created above, if we write the following sql:

create index idx_t1_bcd on t1(b,c,d);

This sql indicates to create an index on t1, the index fields are b, c, d, and are in ascending order, so in fact, the original data is sorted according to the three fields of b, c, and d, then the sorting is similar:

1111a
2222b
5235e
4311d
3322c
7455g
6644f
8888h

You can take a good look. The above records are sorted according to the three fields 1111aof b, c, and d . For example , the values ​​of the three fields of b, c, and d are 111, and the three fields 2222bof b, c, and d are field value is 222, 111less than 222a, the corresponding line of the top surface.

So what are the benefits of sorting the data in this way? In fact, the benefits of sorting by the a field are similar. For example b=4 and c=4 and d=4, the data you want to find now can be queried faster. In fact, this is the principle of indexing:
we create an index on a table, which is The data is sorted, and the sorted data can improve the query speed.

Another point to note is that there are many ways to sort, or some data structures can be used, such as binary trees, red-black trees, and B+ trees. These data structures are actually sorting data, but the sorting forms are different. That's it, each data structure has its own characteristics, and everyone should know that the most used B+ tree in Mysql is still the same.

I believe that, seeing this, everyone should have a new understanding of the index, but the few examples we cited above are all in ascending order, and the sorted data can not only improve the query speed, but also work for order by. For example, if we want to perform t1 now order by b asc,c asc,d asc; for this sorting, if it has been established in the t1 table b,c,d的升序索引, it means that the data in the t1 table has been sorted according to b, c, d in advance, so the order by statement can be directly Use the sorted data, you don't need to use filesort to sort again.

And if our order by is order by b desc, c desc, d desc, we can also use the ascending index of b, c, d, because if it is order by b asc, c asc, da asc, it can be traversed from top to bottom, if it is, order by b desc, c desc, d descit can be traversed from bottom to top. OK.

So, what if order by b asc, c desc, d descit is? Is there no way to use this order by b,c,d的升序索引?

It is needed 降序索引at this time .

Descending index bottom implementation

We spent a lot of space to introduce the implementation principle of ascending index. In summary, the data in the table is sorted in ascending order according to the specified field comparison size .

What is ascending? After the data is compared in size, the smaller is on the top and the big is on the bottom , or if it is a B+ tree, the smaller is on the left and the big is on the right . The descending order means that the big one is on the top and the small one is on the bottom , or if it is a B+ tree, the big one is on the left and the small one is on the right .

So, for the original data above:

4311d
1111a
8888h
2222b
5235e
3322c
7455g
6644f

If we a descsort this data by:

8888h
7455g
6644f
5235e
4311d
3322c
2222b
1111a

It's very simple, if we b desc, c desc, d descsort this data by:

8888h
6644f
7455g
3322c
4311d
5235e
2222b
1111a

It's also very simple. What if we want to b desc, c asc, d descsort this data ? Isn't this a bit embarrassing?

In fact, it is not difficult, sorting is actually comparing the size of the data, we use the following three rows of data to simulate:

3322c
7455g
4311d

First of all, b desc, c desc, d descsort by to get the results as follows:

7455g
3322c
4311d

According b desc, c asc, d descto the order, the results are as follows:

7455g
4311d
3322c

Maybe some big guys can already understand it. In fact, b descwhat it means is that the larger data of the b field is on the top, and the smaller data is on the bottom. If the data is equal, the c field is compared, and the c field is sorted in ascending order, that is, the c field. Those with small data are on the bottom, and those with big data are on the top. So we got the above result.

This is the descending index .

to sum up

The actual ascending index and descending index are different sorting methods. After the descending index is implemented in Mysql8, we are more flexible when creating the index. You can create a suitable index according to the sorting rules required by the business, which can make your query Faster.

Of course, this article only talks about the principle. You must know the B+ tree used for sorting in Mysql, not the simple way I exemplified above, but the principle is the same even if you use the B+ tree, just compare the size of the data.

Another point is that only the Innodb storage engine supports descending indexes.

If you think this article can help you learn knowledge, please click the one-click triple link below, follow + favorite !
If you have any questions about the views in the article, please leave a message in the comment area for discussion.

Guess you like

Origin blog.csdn.net/weixin_51702416/article/details/109221299