Jump to content

ORDER or NOT


jaymc

Recommended Posts

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

Link to comment
Share on other sites

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.

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.