Lots of thread already on web, just trying to understand some nuances which had me confused!
Quoting the doc reference
If you combine LIMIT row_count with ORDER BY, MySQL stops sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast.
and a SO thread
It will order first, then get the first 20. A database will also process anything in the WHERE clause before ORDER BY.
Taking the same query from the question :
SELECT article
FROM table1
ORDER BY publish_date
LIMIT 20
lets say table has 2000 rows, of which query
is expected to return 20 rows, now, looking at mysql ref ....stops sorting as soon as it has found the first row_count rows....
confuses me as i find it little ambiguous!!
Why does it say stops sorting
? isn't the limit
clause being applied on an already sorted data returned via order by
clause ( assuming its a non-indexed
column ) or is my understanding wrong and SQL is limit
ing first and then sorting!!??