I have a query like this where I want to display sorted records in a paginated format.
This is my query.
SELECT * FROM answers
WHERE userid = '10'
ORDER BY votes LIMIT 10, 20
The two arguments to LIMIT will be used for generating page-by-page display of records. Now, my problem is ORDER BY. How does MySQL execute this query?
1st way
- Select the records according to filters
- Sort them
- Limit them
2nd way
- Select the records according to filters
- Limit them
- Sort them
If I think like MySQL engine, I would want to implement 2nd, since I would then have to sort lesser records?
Can somebody throw some light on this?
Regards
A SQL LIMIT will in all databases always work on the result of a query, so it will run the query with the ORDER BY and that result will then be limited. This is a functional requirement, the database not perse needs to execute it like that.
Thus the 1st way.