开发者

MySQL not using indexes; using filesort

开发者 https://www.devze.com 2023-03-17 01:37 出处:网络
开发者_JS百科MySQL appears to be not using indexes and is using filesort on the following query:
开发者_JS百科

MySQL appears to be not using indexes and is using filesort on the following query:

  SELECT `tweets`.* 
    FROM `tweets` 
   WHERE (`tweets`.contest_id = 159) 
ORDER BY tweet_id ASC, tweeted_at DESC LIMIT 100 OFFSET 0

I have indexes on contest_id, tweet_id and tweeted_at

When I execute EXPLAIN EXTENDED, Extra returns "Using where; using filesort". How can I improve my query?


When you mix ASC and DESC sorting, MySQL cannot use indexes to optimize the GROUP BY statement.

Also, using multiple keys to sort will result in it not being able to optimize the query with indexes.

From the docs:

http://dev.mysql.com/doc/refman/5.6/en/order-by-optimization.html

In some cases, MySQL cannot use indexes to resolve the ORDER BY, although it still uses indexes to find the rows that match the WHERE clause. These cases include the following:

You use ORDER BY on different keys:

SELECT * FROM t1 ORDER BY key1, key2;

...

You mix ASC and DESC:

SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

If the two columns you are ordering on are not part of the same key, then you are doing both of the above things.

0

精彩评论

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

关注公众号