Jump to content

[SOLVED] ORDER BY killing


jaymc

Recommended Posts

I have a simple query like so

 

SELECT f.user, f.friend, f.gender, f.x, f.timestamp, c.propic as image

FROM friends f

INNER JOIN cache c ON f.friend = c.username

WHERE f.user = 'jaymc' AND f.gender = 'Male'

ORDER BY f.x DESC, f.timestamp DESC LIMIT 0,10

 

That query takes 100 seconds on a table with 5 million rows

 

If I take the order clause out, it takes under 1 second

 

I really need the order clause, will adding an index on both help? Do I just need to add an index on the f.x field?

 

Please advise

Link to comment
Share on other sites

If you want to make your queries as fast as possible' date=' you should look out for Extra values of [b']Using filesort[/b] and Using temporary.

 

 

* Using filesort

 

MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause. The keys then are sorted and the rows are retrieved in sorted order. See Section 7.2.12, “ORDER BY Optimization”.

Link to comment
Share on other sites

Did you read section 7.2.13 of mySQL docs? There are examples of queries with ORDR BY clause that can, and that cannot benefit from adding indexes. Try to figure out, how to modify your query, and what index to add, so that it was performing better.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.