Descending indexes underlying the new features of Mysql8 achieve

What is descending index

It may be familiar with the index, while descending index unfamiliar, in fact 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);
复制代码

Sql mean, in the above table t1, for b, c, d three fields create a joint index.

But we do not know is actually above and below the sql sql this is equivalent to:

create index idx_t1_bcd on t1(b asc,c asc,d asc);
复制代码

ascIt represents the ascending, use this syntax to create an index called out ascending indexes . That is when we usually create an index, create index is ascending.

You might think, at the time the index is created, you can set asc for the field, it is not desc can also set it?

Of course we can, for example, the following three statements:

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, use this syntax to create an index called out of the descending index , the key question is: just grammatical level of support before Mysql8.0, the bottom does not really support.

We were used Mysql7, Mysql8 two versions here as example to explain:

In Mysql7, Mysql8 create a table, respectively, have a, b, c, d, e five fields:

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

Then create a separate index in descending order:

create index idx_t1_bcd on t1(b desc,c desc,d desc);
复制代码

Once created, we use the following sql look at the index information:

show index from t1;
复制代码

In Mysql7 you will get the results:

image.png

In Mysql8 you will get the results:

image.png

We only care about Key_name to idx_t1_bcd of three rows, careful you should be able to find, the results Collation field in the two results is not the same:

  • In Mysql7, Collation result field is A, A, A, represents b, c, d sort field is three asc
  • In Mysql8, the results Collation field is D, D, D, represents b, c, d is three fields sort desc

But when we create the index, obviously in the syntax level has been designated b, c, d ordering three fields are desc, which can be seen only grammatical level of support in Mysql7 in descending indexes, the underlying does not really support , and is fixed ascending index. In Mysql8 in the real underlying support from the descending indexes.

So far, we should have a general understanding of the ascending and descending index index, but did not really understand, because we do not know ascending and descending indexes underlying index in the end is how to achieve.

Ascending index underlying implementation

We know that the index is used to speed up the search, but why the index can improve query speed?

Given you a number of columns, such as [1,3,7,9,2,5,4,6,8], which is a disorder of the number of columns or arrays, now if you want to increase the number of columns in this query speed, you will first doing what? I believe most people can think of to sort , put that chaotic series, in ascending order of sort, such as get [8, 9], With this order after the number of columns, we can use such algorithms to improve this dichotomy, and so the number of columns in the query speed.

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

So, Mysql data stored in the table is the same, if we want to improve the query speed table, we can first of this data table are sorted , then a row of table data includes many fields, we are now want to sort the rows of these data, we should be determined in accordance with this order which field it? That's index , when you create an index of the specified column is used to sort the data rows of the table.

For example, we still use the t1 table created above, insert data into eight 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 a file, so the format of the data files saved something like, consistent with the sequential order of insertion:

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

Note, t1 is Innodb storage engine, and a field is the primary key, so Innodb storage engine when processing data for these inserted, it will be sorted by the primary key, which is the format of the data I said above, the file saved is not accurate , because do not want too voluminous, so do not get to the bottom, interested students can focus on a wave of public number: 1:25, I will specifically write an article to explain the specific implementation Innodb in the index, including the B + tree in the end is how to generate of.

Based on the above and if we store this way, to find data, such as finding a = line 3 records need to find records from the first row to start looking, you're looking six times, but if we follow the above data a the size of the field to sort:

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

After sorted, if we find this line recorded a = 3, we only need to check 3 times. And so there is a benefit that, if we now need to find a = 3.5 line data, if we based storage Unranked before, we need to query all eight rows finalize a = 3.5 line data does not exist, and if we use storage after sorted, we only need to check four times just fine, because when you found 4311dwhen these rows, you will find 4> 3.5, and can already determine a = 3.5 line is no record It exists.

If we create an index on t1, create an index like the above, if we write is the following sql:

create index idx_t1_bcd on t1(b,c,d);
复制代码

To create the sql to t1 represents an index, the index field is b, c, d, and is ascending, the fact of the original data in accordance with b, c, d sort three fields, the similar after sorting:

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

Can have a good look, is recorded in accordance with the above b, c, d on the three fields to the data line sorting line, such as 1111athe b, c, d value is three fields 111, and 2222bthe b, c, d three field value is 222, 111less than 222a, the corresponding line of the top surface.

If so then the data sorting what good is it? In fact, according to the benefits and just after a similar sort field, such as you now want to find b=4 and c=4 and d=4data also can query faster, in fact, this is the principle of indexing: we create an index on a table, this table is the data is sorted, and sorted data after is the ability to speed up the search.

Another point to note is that there are many in the sort mode, or the data structures may be utilized, such as a binary tree, red-black trees, B + trees data structure is actually sorts the data, only the form of different ordering only, for each data structure has its own characteristics, but we should all know, Mysql most frequently used is the B + tree, or, like, do not want to be too long because of space, interested students can focus on a wave of public number: 1 25 points, I will specifically write an article to explain the specific implementation Innodb in the index, including the B + tree in the end is how to generate.

I believe, see here, we should have a re-understanding of the index, but a few examples we cited above are in ascending order, and row after a good sequence data not only can improve query speed, but also for the order by also useful, for example, if we now want to t1 carried out order by b asc,c asc,d asc;for this sort, if already established in the t1 table b,c,d的升序索引, then the representative data t1 table in advance in accordance with b, c, d sorted, so to order by statements can be used directly row of data has been sorted, do not use filesort sort again.

And if we are in order by order by b desc, c desc, d desc, the same may be utilized ascending index b, c, d, because if order by b asc, c asc, d asc down to it from the traversal, if order by b desc, c desc, d desc it can be traversed from the bottom up.

So, if it is order by b asc, c desc, d desc it? The order by is not no way to use b,c,d的升序索引up.

This time you need descending index up.

Descending index underlying implementation

We spent a great length on the realization of the principle ascending index, the conclusion is that the data in the table in ascending order according to the specified field size comparison .

What Ascending yes? After comparing the data size is small in the large lower , or if it is a B + tree is small on the left, the right large . And that is descending on the big and small in the next , or if it is B + tree, then that is great in the left, a small right .

Therefore, for the above share of raw data:

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

If we follow this data a descsorting is:

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

It is very simple, that if we follow this data b desc, c desc, d descsorting is:

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

Is also very simple, that if we want this data in accordance with the b desc, c asc, d descsorting of it? This is not a bit muddled?

It is not difficult, in fact, the sort of data size comparison, we use the following three lines of data to simulate the look:

3322c
7455g
4311d
复制代码

First, in accordance with b desc, c desc, d desc sort, to give the following results:

7455g
3322c
4311d
复制代码

According to b desc, c asc, d desc sort, to give the following results:

7455g
4311d
3322c
复制代码

May have a portion of the big brother can be appreciated, in fact, b descthe meaning is expressed by b on the large data field, the following data were small, equal comparison is started, then the data fields c and c is the field in ascending row, i.e. c Field small person next data, data on the larger. So we get the results of the above.

This is the descending index .

to sum up

Indeed ascending and descending index index is just a different sort, Mysql8 being achieved after descending index, we are more flexible when creating an index, you can create the appropriate index based on the collation business needs, so make your queries faster.

Of course, this paper talk about the principle, we must know the sort Mysql in the use of B + trees, not above me give an example of the kind of very simple way, but even with a B + tree principle is the same, compare the size of data only.

Another point, now only Innodb storage engine supports descending indexes .

If you find this article allows you to acquire knowledge, can help forward, will share out knowledge. If you want the first time to learn more exciting content, please pay attention to micro-channel public number: 1:25

Guess you like

Origin juejin.im/post/5d11d923e51d45777b1a3dc4