在「MySQL 8.0 Labs – Descending Indexes in MySQL」這邊看到 MySQL 打算在 8.0 時實作出真正的 Descending Indexes。在 5.7 以及之前的版本,可以從「14.1.14 CREATE INDEX Syntax」看到這個參數是~假~的~XDDD
An index_col_name specification can end with ASC or DESC. These keywords are permitted for future extensions for specifying ascending or descending index value storage. Currently, they are parsed but ignored; index values are always stored in ascending order.
所以當 8.0 建立了 a_desc_b_asc (a DESC, b ASC)
這樣的 index,可以看到對於不同 ORDER BY
時效能的差異:(一千萬筆資料)
有些變快可以理解,但有些結果不太清楚造成的原因...
Anyway,對於變慢的兩個 query,他提了一個不算解法的解法,就是加上對應的 index XDDD:
If user wants to avoid filesorts for Query 5 and Query 6, he/she can alter the table to add a key (a ASC, b ASC) . Further to this, if the user wants to avoid backward index scans too, he/she can add both ( a ASC, b DESC) and (a DESC, b DESC).
這樣就會變快,但寫入的 overhead 會增加啊... XD
但不管怎樣,總算是把這個功能生出來了...