一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - MySQL8新特性:降序索引詳解

MySQL8新特性:降序索引詳解

2019-07-02 17:36iVictor Mysql

在數據庫中我們一般都會對一些字段進行索引操作,這樣可以提升數據的查詢速度,下面這篇文章主要給大家介紹了關于MySQL8新特性:降序索引的相關資料,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧

前言

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.7

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
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
row in set (0.00 sec)

c2列還是保留了desc子句。

降序索引的意義

如果一個查詢,需要對多個列進行排序,且順序要求不一致。在這種場景下,要想避免數據庫額外的排序-“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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-----------------------------+
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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
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/

 MySQL8新特性:降序索引詳解

而在8.0中,對于反向掃描,有一個專門的詞進行描述“Backward index scan”。

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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
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 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+----------------------------------+
row in set, 1 warning (0.00 sec)

終于不再對group by進行隱式排序

由于降序索引的引入,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 |
+------+
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 |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+
row in set, 1 warning (0.00 sec)

“Using filesort”,代表查詢中有排序操作,從結果上看,id列確實也是升序輸出。

MySQL 8.0

mysql> select * from slowtech.t1 group by id;
+------+
| id |
+------+
| 2 |
| 3 |
| 1 |
+------+
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 |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
row in set, 1 warning (0.01 sec)

不僅結果沒有升序輸出,執行計劃中也沒有“Using filesort”。

可見,MySQL 8.0對于group by操作確實不再進行隱式排序。

從5.7升級到8.0,依賴group by隱式排序的業務可要小心咯。

參考文檔

http://mysqlserverteam.com/mysql-8-0-labs-descending-indexes-in-mysql/

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片区 | 男人操女人动图 | 激情另类国内一区二区视频 | 欧美成人v视频免费看 | 接吻吃胸摸下面啪啪教程 | 人与动人物人a级特片 | 人人精品久久 | 国产伦精品一区二区三区免 | 成人高清视频在线观看 | 男人晚上看的 | 忘忧草在线社区WWW日本-韩国 | 欧美国产日韩1区俺去了 | 狠狠干2017| 亚洲美色综合天天久久综合精品 | 99爱在线精品视频免费观看9 | 国产 日韩欧美 | 久久青草免费91线频观看站街 | 成人网18免费网站 | 四虎欧美 | 精品久久久久久久久久久久久久久 | 国产成人永久免费视 | 美女鸡| 国产精品一区久久精品 | 男同桌扒开女同桌胸罩喝奶 | 欧美粗黑巨大gay | 国产草草| 亚洲天堂日韩在线 | 成年女人毛片免费观看中文w | 国产精品永久免费10000 | 国产美女下面流出白浆视频 | 婷婷在线观看香蕉五月天 | 久久re这里精品在线视频7 | 色老板在线观看 | 国产成人免费高清激情明星 | 亚洲精品卡1卡二卡3卡四卡 | 国产成人精品免费大全 | 国产精品免费久久久久影院小说 | 欧美福利二区 | 爱情岛论坛亚洲品质自拍视频 | ysav67| 范冰冰a级一级特级毛片 |