MySQL8新特性:降序索引詳解
前言
MySQL 8.0終于支持降序索引了。其實(shí),從語法上,MySQL 4就支持了,但正如官方文檔所言,"they are parsed but ignored",實(shí)際創(chuàng)建的還是升序索引。
無圖無真相,同一個(gè)建表語句,看看MySQL 5.7和8.0的區(qū)別。
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,但在實(shí)際的建表語句中還是將其忽略了。再來看看MySQL 8.0的結(jié)果。
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子句。
降序索引的意義
如果一個(gè)查詢,需要對(duì)多個(gè)列進(jìn)行排序,且順序要求不一致。在這種場景下,要想避免數(shù)據(jù)庫額外的排序-“filesort”,只能使用降序索引。還是上面這張表,來看看有降序索引和沒有的區(qū)別。
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)
兩者的對(duì)比可以看出,MySQL 8.0因?yàn)榻敌蛩饕拇嬖冢苊饬恕癴ilesort”。
這其實(shí)是降序索引的主要應(yīng)用場景。如果只對(duì)單個(gè)列進(jìn)行排序,降序索引的意義不是太大,無論是升序還是降序,升序索引完全可以應(yīng)付。還是同樣的表,看看下面的查詢。
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是升序索引,但在第二個(gè)查詢中,對(duì)其進(jìn)行降序排列時(shí),并沒有進(jìn)行額外的排序,使用的還是索引。在這里,大家容易產(chǎn)生誤區(qū),以為升序索引就不能用于降序排列,實(shí)際上,對(duì)于索引,MySQL不僅支持正向掃描,還可以反向掃描。反向掃描的性能同樣不差。以下是官方對(duì)于降序索引的壓測結(jié)果,測試表也只有兩列(a,b),建了一個(gè)聯(lián)合索引(a desc,b asc),感興趣的童鞋可以看看,http://mysqlserverteam.com/mysql-8-0-labs-descending-indexes-in-mysql/
而在8.0中,對(duì)于反向掃描,有一個(gè)專門的詞進(jìn)行描述“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)
終于不再對(duì)group by進(jìn)行隱式排序
由于降序索引的引入,MySQL 8.0再也不會(huì)對(duì)group by操作進(jìn)行隱式排序。
下面看看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”,代表查詢中有排序操作,從結(jié)果上看,id列確實(shí)也是升序輸出。
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)
不僅結(jié)果沒有升序輸出,執(zhí)行計(jì)劃中也沒有“Using filesort”。
可見,MySQL 8.0對(duì)于group by操作確實(shí)不再進(jìn)行隱式排序。
從5.7升級(jí)到8.0,依賴group by隱式排序的業(yè)務(wù)可要小心咯。
參考文檔
http://mysqlserverteam.com/mysql-8-0-labs-descending-indexes-in-mysql/
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
MySql 存儲(chǔ)引擎和索引相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了MySql 存儲(chǔ)引擎和索引相關(guān)知識(shí)總結(jié),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06mysql ONLY_FULL_GROUP_BY設(shè)置sql_mode無效排查問題(windows)
這篇文章主要介紹了mysql ONLY_FULL_GROUP_BY設(shè)置sql_mode無效排查問題(windows),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09用SQL實(shí)現(xiàn)統(tǒng)計(jì)報(bào)表中的"小計(jì)"與"合計(jì)"的方法詳解
本篇文章是對(duì)使用SQL實(shí)現(xiàn)統(tǒng)計(jì)報(bào)表中的"小計(jì)"與"合計(jì)"的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06優(yōu)化mysql之key_buffer_size設(shè)置
這篇文章主要介紹了優(yōu)化mysql之key_buffer_size設(shè)置的相關(guān)資料,需要的朋友可以參考下2016-05-05mysql 5.7.13 winx64安裝配置方法圖文教程(win10)
這篇文章主要為大家分享了mysql 5.7.13 winx64安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01在MySQL中創(chuàng)建帶有IN和OUT參數(shù)的存儲(chǔ)過程的方法
這篇文章主要介紹了在MySQL中創(chuàng)建帶有IN和OUT參數(shù)的存儲(chǔ)過程的方法,在一定程度上簡化了操作,需要的朋友可以參考下2015-06-06MySQL8.4一主一從環(huán)境搭建實(shí)現(xiàn)
本文主要介紹了MySQL8.4一主一從環(huán)境搭建實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06