简体   繁体   English

我应该使用哪个索引来混合WHERE和ORDER BY

[英]Which index should I use for mix WHERE and ORDER BY

I have following query 我有以下查询

SELECT * FROM mytable WHERE toshow = 1 ORDER BY timestamp DESC

The timestamp column stores current timestamp in database when inserting row. 插入行时,时间戳列会将当前时间戳存储在数据库中。 In some rows 'toshow' value is 0 and in some rows its 1 在某些行中,“ toshow”值为0,在某些行中,其值为1

What INDEX should I use to get results faster for any 0 or 1 toshow value and latest row above 对于任何0或1,我应该使用什么INDEX来更快地显示结果,并显示上面的最新行

I created index (timestamp, toshow) for this but confusing its right or not. 我为此创建了索引(timestamp, toshow) ,但是混淆了它的正确与否。

覆盖指数:

CREATE INDEX idx ON mytable(toshow, timestamp DESC);

You shoul use the where part first as the most selective part so could be easy used by the where clause 您应该首先使用where部分作为最有选择性的部分,以便where子句可以轻松使用

 index (toshow, timestamp DESC ) 

the timestamp column is used for provide the info for order by but this happen after the filering for the rows based on where clause timestamp列用于提供排序依据,但是这种情况发生在基于where子句对行进行筛选之后

In this way the index It is used both to filter the lines and to obtain the formats for the sorting in your way the index can't be used for where .. 通过这种方式,索引It既可以用来过滤行,又可以以您无法将索引用于位点的方式获得排序的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM