Does adding 'LIMIT 1' to MySQL queries make them faster when you know there will only be 1 result?

Logan Serman picture Logan Serman · Jan 18, 2009 · Viewed 30.2k times · Source

When I add LIMIT 1 to a MySQL query, does it stop the search after it finds 1 result (thus making it faster) or does it still fetch all of the results and truncate at the end?

Answer

Eran Galperin picture Eran Galperin · Jan 18, 2009

Depending on the query, adding a limit clause can have a huge effect on performance. If you want only one row (or know for a fact that only one row can satisfy the query), and are not sure about how the internal optimizer will execute it (for example, WHERE clause not hitting an index and so forth), then you should definitely add a LIMIT clause.

As for optimized queries (using indexes on small tables) it probably won't matter much in performance, but again - if you are only interested in one row than add a LIMIT clause regardless.