簡體   English   中英

MySQL何時將對使用order by子句的查詢應用索引條件推送?為什么?

[英]When will MySQL apply Index Condition Push for a query with order by clause?Why?

我有一個帶有多列索引(author_id,reply_time,id)的文章表。

explain select * from article where author_id=3658768 and reply_time>'2015-01-01' and id>85669107 order by reply_time
explain select * from article where author_id=3658768 and reply_time>'2015-01-01' and id<85669107 order by reply_time
explain select * from article where author_id=3658768 and reply_time<'2015-01-01' and id>85669107 order by reply_time
explain select * from article where author_id=3658768 and reply_time<'2015-01-01' and id<85669107 order by reply_time

將使用索引條件推送(說明輸出的“額外”字段為“使用索引條件”)

但是,以下所有查詢將不使用索引條件推送(explain輸出的“ extra”字段為“ Using where”)

explain select * from article where author_id=3658768 and reply_time>'2015-01-01' and id>85669107 order by reply_time desc
explain select * from article where author_id=3658768 and reply_time>'2015-01-01' and id<85669107 order by reply_time desc
explain select * from article where author_id=3658768 and reply_time<'2015-01-01' and id>85669107 order by reply_time desc
explain select * from article where author_id=3658768 and reply_time<'2015-01-01' and id<85669107 order by reply_time desc

MySQL是否僅按索引順序使用索引條件推送?

通過author_id, reply_time, id進行索引幾乎沒有意義author_id, reply_time, id除非您確定同一author_id, reply_time會有多個id 索引不會比僅通過author_id, reply_timeauthor_id, reply_time更好。

您最好使用兩個索引,一個通過author_id, reply_time和另一個通過author_id, id索引,並且計划者將使用最有可能(從收集的統計數據中)得出的索引數量將減少的行數。按順序過濾。

不管排序是升序還是降序都是無關緊要的,計划者可以利用索引進行排序,也可以很好地使用它進行升序或降序。 但是有時它根本無法使用它,或者使用它對行集進行排序最昂貴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM