MySQL何时将对使用order by子句的查询应用索引条件推送?为什么?

YCFlame

我有一个带有多列索引(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是否仅按索引顺序使用索引条件推送?

埃泽奎尔·托尔奈(Ezequiel Tolnay)

author_id, reply_time, id除非您确定id相同会有多个s,否则使用索引by几乎没有意义author_id, reply_time该指数不会比author_id, reply_time单独存在更好

您最好使用两个索引,一个索引一个索引,author_id, reply_time另一个索引一个索引,author_id, id计划者将使用最有可能(从收集到的统计信息中)的索引,该索引将呈现较少数量的必须顺序过滤的行。

不管排序是升序还是降序都是无关紧要的,计划者可以利用索引进行排序,也可以将索引用于升序或降序。但是有时它根本无法使用它,或者使用它对行集进行排序最昂贵。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么如果我在WHERE子句上再添加一个条件,则使用索引失败?

当我使用“OR”条件时,为什么我的查询使用过滤而不是索引条件?

MySQL:在where子句中将索引应用于具有4个用户控制条件的受益查询

为什么Postgresql不对IN查询使用索引?

为什么此查询不使用索引?

为什么在创建索引时使用INCLUDE子句?

为什么 MySQL 不在此查询中使用我的索引?

为什么在条件类型的extends子句中使用元组

MySQL查询解释:查询何时具有“ WHERE”和“ ORDER BY”的索引选择

为什么在MySQL中将LIMIT子句放在ORDER子句之后?

MySQL使用索引改进GROUP BY ORDER BY查询

使用Rownum和order by子句查询不使用索引

当where子句仅包含索引的第一列时,为什么不使用索引?

在MySQL查询中使用varchar类型的MySQL order by子句

为什么在MySQL中使用子查询别名在“ where子句”中获取“未知”列?

mysql-为什么在WHERE子句上使用此子查询?

MySQL将“ ORDER BY”子句应用于UNION查询

为什么使用MySQL“ WHERE”子句逼近:即使不满足条件也检索值

为什么这个简单的查询不使用postgres中的索引?

PostgreSQL为什么不为此联接查询使用索引

为什么PostgreSQL在此查询中不使用索引

为什么我的SQL查询未使用表的复合索引?

为什么索引不与子查询一起使用

为什么 MongoDB 不使用复合索引进行查询?

为什么PostgreSQL在“ WHERE NOT IN”条件下不使用索引。

即使索引内有条件,为什么还要使用“索引条件”?

为什么ILIN ANY子句不使用GIN三元组索引?

sort_values 与 order by 以及何时以及为什么我应该使用哪个

Eloquent 查询不使用 where/first 条件。为什么?