开发者

Is there a way to use the index to avoid a filesort when OR is used in MySQL query?

I have the following query and the following 2 indexes, but I am still getting a filesort. Is there a 开发者_StackOverflowway to optimize this further so that I avoid the filesort, or do I just need to live with it?

Query:

SELECT * FROM table WHERE row1 > '0' OR row2 = '1' ORDER BY id DESC

Indexes:

row1

row2,id

EXPLAIN output:

Using sort_union(row1,row2_id); Using where; Using filesort


ORDER BY ROW2,ID or add an index on ID.


Yes, you can. Just add index like (id, row1, row2).

This index will work, because rows in it will be pre-sorted and mySQL will only need to select the satisfying lines.

Moreover, sort_union() index merging is not THAT good optimization for mySQL anyway.


It's a little bit late for answering but i want to say that, using "or" probably causing that problem. You can try using union with two different queries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜