jaymc Posted March 6, 2007 Share Posted March 6, 2007 I have a query that is going to look for 900 rows in a table that has 150,000 rows Now, if i query the table with a LIMIT 0,900 it will go through every row until it finds 900 that match its criteria So in theory it may check 100,000 rows to find its 900 However, I know that if I ORDER the table by `lastaction` those 900 rows will all be at the bottom Will it slow the query down more to use ORDER or to have it check all those rows until it finds its 900 matches Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/ Share on other sites More sharing options...
btherl Posted March 6, 2007 Share Posted March 6, 2007 Have you timed the queries? If you query without ordering, then mysql will simply return the first 900 rows (as stored on disk), which will be quite fast. That's fine as long as the first 900 rows are what you want. If you use order by, mysql can use an index on the order condition to find the first 900 rows in that ordering. I would expect it to be slower though, as each row may be in different pages on disk, and the index must also be read in. Benchmark it and see An order by without an index will be much much slower, as it must sort the entire table before taking the first 900. Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-200467 Share on other sites More sharing options...
jaymc Posted March 6, 2007 Author Share Posted March 6, 2007 So in theory ORDER would slow it down? Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-201000 Share on other sites More sharing options...
artacus Posted March 6, 2007 Share Posted March 6, 2007 Yes, it probably will. Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-201081 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 But I'm unclear as to why you have the "choice" to use the order by. Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-201406 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 Because the way records are entered into the db creates a pseudo chronological order by. Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-201421 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Ah yes, we've encountered this before. Quote Link to comment https://forums.phpfreaks.com/topic/41366-order-or-not/#findComment-201836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.