色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

MySQL8.0中的降序索引

瀏覽:2日期: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 數據庫
相關文章:
主站蜘蛛池模板: 狠狠五月深爱婷婷网 | 美日韩一区二区 | 99久久在线 | 色站综合 | 久久精品国产99久久 | 美女视频黄.免费网址 | 久久久久香蕉视频 | 国产在线高清视频 | 高颜值美女啪啪 | 欧美人成在线观看ccc36 | 亚洲日本一区二区三区高清在线 | 亚洲影院手机版777点击进入影院 | 国产免费久久 | 久久久久久久91精品免费观看 | 三级手机在线观看 | 美女视频黄a全部免费专区一 | 久久国产精品夜色 | 久久视频国产 | 九九久久精品视频 | 日本一本色道 | 久久国产一级毛片一区二区 | 欧美二区在线观看 | 成年人在线视频免费观看 | 欧美亚洲日本国产综合网 | 日韩高清不卡在线 | 91成人在线免费观看 | 久久久精品久久视频只有精品 | 1769视频在线观看国产 | 欧美性久久久久 | 成人在线免费网站 | 亚洲国产精品网 | 91福利精品老师国产自产在线 | 一区二区国产在线观看 | 亚洲国产99在线精品一区二区 | 毛片在线免费播放 | 韩国福利影视一区二区三区 | 鲁大师成人精品视频 | 欧美日a | 成熟性xxxxx| 欧美巨乳在线 | 国产欧美一区二区三区视频在线观看 |