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
https://forums.phpfreaks.com/topic/120097-solved-order-by-killing/
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”.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.