By suggestion of @RolandoMySQLDBA, I'm combining my comments into an answer.
So, in many cases, LIMIT
clause enables the database server to do less work before delivering the result. MySQL docs explain such situations.
It is best to check queries with EXPLAIN
to see the execution plan. If the execution plan is different with and without limit clause, then some optimizations were performed (query can be executed more efficiently because of limit by clause, or at least the optimizer thinks so).
For example, using the Sakila test database: a table film
has a column named title
which is indexed. It has 1000 rows. EXPLAIN
for query
select * from film order by title
shows that the databases reads and sorts the entire table (this being cheaper than using the index on title and accessing table rows from index entries, since the whole table must be read). On the other hand, EXPLAIN
for the same query with LIMIT
clause,
select * from film order by title limit 10
shows that the database now decides to use index on title
column; it only needs to read the first 10 index entries and retrieve rows that those entries point to. The rest of the index or table are never accessed. Certainly much cheaper than the first query, if you only need the first 10 rows.