MySQL 8 新特性之降序索引

MySQL 8.0終於支持降序索引了。其實,從語法上,MySQL 4就支持了,但正如官方文檔所言,"they are parsed but ignored",實際建立的仍是升序索引。
 
無圖無真相,同一個建表語句,看看MySQL 5.7和8.0的區別。
create table slowtech.t1(c1 int,c2 int,index idx_c1_c2(c1,c2 desc));

 

MySQL 5.7mysql

mysql> show create table slowtech.t1\G
*************************** 1. row ***************************
      Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  KEY `idx_c1_c2` (`c1`,`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
 
雖然c2列指定了desc,但在實際的建表語句中仍是將其忽略了。再來看看MySQL 8.0的結果。 
 
mysql> show create table slowtech.t1\G
*************************** 1. row ***************************
      Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  KEY `idx_c1_c2` (`c1`,`c2` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

c2列仍是保留了desc子句。sql

 

降序索引的意義
若是一個查詢,須要對多個列進行排序,且順序要求不一致。在這種場景下,要想避免數據庫額外的排序-「filesort」,只能使用降序索引。仍是上面這張表,來看看有降序索引和沒有的區別。
 
MySQL 5.7
mysql> explain select * from slowtech.t1 order by c1,c2 desc;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-----------------------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra                      |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-----------------------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)

 

MySQL 8.0
mysql> explain select * from slowtech.t1 order by c1,c2 desc;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra      |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

 

二者的對比能夠看出,MySQL 8.0由於降序索引的存在,避免了「filesort」。
 
這實際上是降序索引的主要應用場景。若是隻對單個列進行排序,降序索引的意義不是太大,不管是升序仍是降序,升序索引徹底能夠應付。仍是一樣的表,看看下面的查詢。
 
MySQL 5.7
mysql> explain select * from slowtech.t1 order by c1;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra      |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from slowtech.t1 order by c1 desc;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra      |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

 

雖然c1是升序索引,但在第二個查詢中,對其進行降序排列時,並無進行額外的排序,使用的仍是索引。在這裏,你們容易產生誤區,覺得升序索引就不能用於降序排列,實際上,對於索引,MySQL不只支持正向掃描,還能夠反向掃描。反向掃描的性能一樣不差。如下是官方對於降序索引的壓測結果,測試表也只有兩列(a,b),建了一個聯合索引(a desc,b asc),感興趣的童鞋能夠看看,http://mysqlserverteam.com/mysql-8-0-labs-descending-indexes-in-mysql/數據庫

 

而在8.0中,對於反向掃描,有一個專門的詞進行描述「Backward index scan」。
 
MySQL 8.0
mysql> explain select * from slowtech.t1 order by c1;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra      |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from slowtech.t1 order by c1 desc;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+----------------------------------+
| id | select_type | table | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra                            |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+----------------------------------+
|  1 | SIMPLE      | t1    | NULL      | index | NULL          | idx_c1_c2 | 10      | NULL |    1 |  100.00 | Backward index scan; Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+----------------------------------+
1 row in set, 1 warning (0.00 sec)

 

終於再也不對group by進行隱式排序sqlserver

因爲降序索引的引入,MySQL 8.0不再會對group by操做進行隱式排序。
下面看看MySQL 5.7和8中的測試狀況
 
 
create table slowtech.t1(id int);
insert into slowtech.t1 values(2);
insert into slowtech.t1 values(3);
insert into slowtech.t1 values(1);

 

MySQL 5.7性能

mysql> select * from slowtech.t1 group by id;
+------+
| id  |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysql> explain select * from slowtech.t1 group by id;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
|  1 | SIMPLE      | t1    | NULL      | ALL  | NULL          | NULL | NULL    | NULL |    3 |  100.00 | Using temporary; Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
1 row in set, 1 warning (0.00 sec)

「Using filesort」,表明查詢中有排序操做,從結果上看,id列確實也是升序輸出。測試

 

MySQL 8.0spa

mysql> select * from slowtech.t1 group by id;
+------+
| id  |
+------+
|    2 |
|    3 |
|    1 |
+------+
3 rows in set (0.00 sec)

mysql> explain select * from slowtech.t1 group by id;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
|  1 | SIMPLE      | t1    | NULL      | ALL  | NULL          | NULL | NULL    | NULL |    3 |  100.00 | Using temporary |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
1 row in set, 1 warning (0.01 sec)

不只結果沒有升序輸出,執行計劃中也沒有「Using filesort」。code

可見,MySQL 8.0對於group by操做確實再也不進行隱式排序。server

從5.7升級到8.0,依賴group by隱式排序的業務可要當心咯。
 
參考文檔
相關文章
相關標籤/搜索