2

i couldn't find a way to optimize the following Query:

SELECT * 
FROM tbl 
WHERE type='51' AND `start`<='2012-01-19' 
ORDER BY end DESC 
LIMIT 5

I've tried by indexing each column in a separate index (type,start,end), and all of them in the same index, but MySQL keeps telling me that needs to do a filesort

Is this query just impossible to optimize?

2
  • 2
    How many rows are in the table? Commented Jan 19, 2012 at 19:39
  • The table contains about 40.000 Rows Commented Jan 19, 2012 at 19:45

5 Answers 5

1

Yes, as long you have range comparison in WHERE and sort by another field - mysql cannot use index for sorting.

It could if you had WHERE type='51' ANDstart='2012-01-19' ORDER BY end DESC or WHERE type='51' ANDstart<= '2012-01-19' ORDER BY start DESC

http://dev.mysql.com/doc/refman/5.5/en/order-by-optimization.html -- and here is a chapter relevant to your problem

Sign up to request clarification or add additional context in comments.

Comments

0

It depends greatly on the column types and what's all in the table, but all you should really need are indexes on type, start, and end columns.

For an extra boost, you can make an index across type and start.

1 Comment

Separate indexes won't help. Mysql can only use one index at a time
0

My first thought is (*) is expanding into rows that are not indexed.

If not, you will most certainly benefit from a Multiple-Column Index.

I would experiment and learn to see how to create the order of columns in the index definition with the lowest cardinality (approx unique combinations) of the columns.

3 Comments

"I would experiment and learn to see how to create the order of columns with the lowest cardinality (approx unique combinations) of the columns." --- and it has nothing to do with original question
Would you have preferred my response to be "No, it is not impossible to optimize" : only answering the 'actual' question?
I have already given an answer. And your answer contains irrelevant information. Yes, it is better to say just NO instead of giving advices that aren't useful in any way
0

Thanks for all your answers, it made me learn a lot about the issue.

And finally i got it! Looks like this is actually possible to Optimize, or at least to remove that using filesort in the EXPLAIN sentence.

Here's the indexes i used:

    KEY `start` (`start`),
    KEY `typeend` (`type`,`end`)

Now executing:

    EXPLAIN  SELECT * 
    FROM tbl 
    WHERE type='51' AND `start`<='2012-01-19' 
    ORDER BY end 
    DESC LIMIT 5

Leads to:

SIMPLE  tbl ref start,type,typeend  typeend 5   const   19  Using where

2 Comments

How long does in take now compared to the time in the question?
Well, the timing is almost the same because here we're dealing with a really small amount of data (40K rows), the whole table sizes less than 3MB, but i thought it was a great example to ask, because i have many more cases like this with bigger and complex tables, and now i'll be able to start dealing with them
0

I would recommend the following:

  1. add index (type, end, start)

  2. rewrite the query:

    SELECT *
    FROM (
      SELECT id -- `id` is the primary key
      FROM tbl
      WHERE type='51' AND `start`<='2012-01-19' 
      ORDER BY end DESC
      LIMIT 5) as ids
    JOIN tbl
    USING (id); -- `id` is the primary key
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.