亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

MySQL8.0中的降序索引

瀏覽:15日期:2023-10-10 07:32:37

前言

相信大家都知道,索引是有序的;不過,在MySQL之前版本中,只支持升序索引,不支持降序索引,這會帶來一些問題;在最新的MySQL 8.0版本中,終于引入了降序索引,接下來我們就來看一看。

降序索引

單列索引

(1)查看測試表結構

mysql> show create table sbtest1G*************************** 1. row *************************** Table: sbtest1Create Table: CREATE TABLE `sbtest1` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `k` int unsigned NOT NULL DEFAULT ’0’, `c` char(120) NOT NULL DEFAULT ’’, `pad` char(60) NOT NULL DEFAULT ’’, PRIMARY KEY (`id`), KEY `k_1` (`k`)) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci MAX_ROWS=10000001 row in set (0.00 sec)

(2)執行SQL語句order by ... limit n,默認是升序,可以使用到索引

mysql> explain select * from sbtest1 order by k limit 10;+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | k_1 | 4 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)

(3)執行SQL語句order by ... desc limit n,如果是降序的話,無法使用索引,雖然可以相反順序掃描,但性能會受到影響

mysql> explain select * from sbtest1 order by k desc limit 10;+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra|+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | k_1 | 4 | NULL | 10 | 100.00 | Backward index scan |+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+1 row in set, 1 warning (0.00 sec)

(4)創建降序索引

mysql> alter table sbtest1 add index k_2(k desc);Query OK, 0 rows affected (6.45 sec)Records: 0 Duplicates: 0 Warnings: 0

(5)再次執行SQL語句order by ... desc limit n,可以使用到降序索引

mysql> explain select * from sbtest1 order by k desc limit 10;+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | k_2 | 4 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)

多列索引

(1)查看測試表結構

mysql> show create table sbtest1G*************************** 1. row *************************** Table: sbtest1Create Table: CREATE TABLE `sbtest1` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `k` int unsigned NOT NULL DEFAULT ’0’, `c` char(120) NOT NULL DEFAULT ’’, `pad` char(60) NOT NULL DEFAULT ’’, PRIMARY KEY (`id`), KEY `k_1` (`k`), KEY `idx_c_pad_1` (`c`,`pad`)) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci MAX_ROWS=10000001 row in set (0.00 sec)

(2)對于多列索引來說,如果沒有降序索引的話,那么只有SQL 1才能用到索引,SQL 4能用相反順序掃描,其他兩條SQL語句只能走全表掃描,效率非常低

SQL 1:select * from sbtest1 order by c,pad limit 10;

SQL 2:select * from sbtest1 order by c,pad desc limit 10;

SQL 3:select * from sbtest1 order by c desc,pad limit 10;

SQL 4:explain select * from sbtest1 order by c desc,pad desc limit 10;

mysql> explain select * from sbtest1 order by c,pad limit 10;+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | idx_c_pad_1 | 720 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)mysql> explain select * from sbtest1 order by c,pad desc limit 10;+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+| 1 | SIMPLE | sbtest1 | NULL | ALL | NULL | NULL | NULL | NULL | 950738 | 100.00 | Using filesort |+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+1 row in set, 1 warning (0.00 sec)mysql> explain select * from sbtest1 order by c desc,pad limit 10;+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+| 1 | SIMPLE | sbtest1 | NULL | ALL | NULL | NULL | NULL | NULL | 950738 | 100.00 | Using filesort |+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+1 row in set, 1 warning (0.01 sec)mysql> explain select * from sbtest1 order by c desc,pad desc limit 10;+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra|+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | idx_c_pad_1 | 720 | NULL | 10 | 100.00 | Backward index scan |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+1 row in set, 1 warning (0.00 sec)

(3)創建相應的降序索引

mysql> alter table sbtest1 add index idx_c_pad_2(c,pad desc);Query OK, 0 rows affected (1 min 11.27 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table sbtest1 add index idx_c_pad_3(c desc,pad);Query OK, 0 rows affected (1 min 14.22 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table sbtest1 add index idx_c_pad_4(c desc,pad desc);Query OK, 0 rows affected (1 min 8.70 sec)Records: 0 Duplicates: 0 Warnings: 0

(4)再次執行SQL,均能使用到降序索引,效率大大提升

mysql> explain select * from sbtest1 order by c,pad desc limit 10;+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | idx_c_pad_2 | 720 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)mysql> explain select * from sbtest1 order by c desc,pad limit 10;+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | idx_c_pad_3 | 720 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)mysql> explain select * from sbtest1 order by c desc,pad desc limit 10;+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+| 1 | SIMPLE | sbtest1 | NULL | index | NULL | idx_c_pad_4 | 720 | NULL | 10 | 100.00 | NULL |+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+1 row in set, 1 warning (0.00 sec)

總結

MySQL 8.0引入的降序索引,最重要的作用是,解決了多列排序可能無法使用索引的問題,從而可以覆蓋更多的應用場景。

以上就是MySQL8.0中的降序索引的詳細內容,更多關于MySQL 降序索引的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 亚洲jjzzjjzz在线播放 | 亚欧在线精品免费观看一区 | 1024在线视频国产在线播放 | 在线观看视频黄色 | 免费在线看h| 香蕉视频在线播放 | 亚洲欧美网址 | 国产精品人成在线播放新网站 | 韩国 欧美 日产 国产精品 | 精品国产女同疯狂摩擦2 | 一区卡二区卡三区卡视频 | 国产 日韩 欧美 综合 | 九九视频高清视频免费观看 | 欧美精品伊人久久 | 中文字幕一二三四区 | 久久电影精品 | 国产精品视频国产永久视频 | 黄的三级在线播放 | 黄色毛片电影黄色毛片 | 成年人在线观看视频网站 | 美国免费高清一级毛片 | 特黄特黄特色大片免费观看 | 国产在线一区在线视频 | 久久久99精品久久久 | 色在线影院 | 国产一二三区在线观看 | 高清视频欧美性生活 | 打美眉屁股v7.3 | 视频在线观看一区 | 日本一级特黄特色大片免费视频 | 国产v综合v亚洲欧美大片 | 日本毛片在线看 | 成人1000部免费观看视频 | 日本一线a视频免费观看 | 久久精品一区二区三区日韩 | 香蕉视频视频 | 97青青草原国产免费观看 | 91一区二区三区四区五区 | 亚洲 欧美 激情 另类 自拍 | 最新色图 | 婷婷在线网 |