For example, I'm querying on a field I know will be unique and is indexed such as a primary key. Hence I know this query will only return 1 row (even without the LIMIT 1)
SELECT * FROM tablename WHERE tablename.id=123 LIMIT 1
or only update 1 row
UPDATE tablename SET somefield='somevalue' WHERE tablename.id=123 LIMIT 1
Would adding the LIMIT 1
improve query execution time if the field is indexed?
The LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
The answer, in short, is yes.If you limit your result to 1, then even if you are "expecting" one result, the query will be faster because your database wont look through all your records. It will simply stop once it finds a record that matches your query.
MySQL provides a LIMIT clause that is used to specify the number of records to return. The LIMIT clause makes it easy to code multi page results or pagination with SQL, and is very useful on large tables. Returning a large number of records can impact on performance.
The LIMIT clause can restrict the result set of the query to some maximum number of rows. If this clause specifies a value smaller than the number of qualifying rows, the query returns only a subset of the rows that satisfy the selection criteria.
It is not good practice to use LIMIT 1
when querying with filter criteria that is against either a primary key or unique constraint. A primary key, or unique constraint, means there is only one row/record in the table with that value, only one row/record will ever be returned. It's contradictory to have LIMIT 1
on a primary key/unique field--someone maintaining the code later could mistake the importance & second guess your code.
But the ultimate indicator is the explain plan:
explain SELECT t.name FROM USERS t WHERE t.userid = 4
...returns:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra ----------------------------------------------------------------------------------------------------- 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 |
...and:
explain SELECT t.name FROM USERS t WHERE t.userid = 4 LIMIT 1
...returns:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra ----------------------------------------------------------------------------------------------------- 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 |
No difference, no need. It appears to be optimized out in this case (only searching against the primary key).
An indexed field doesn't guarantee uniqueness of the value being filtered, there could be more than one occurrence. So LIMIT 1
would make sense, assuming you want to return one row.
If the field has a unique index on it, I can't see this improving execution time at all - it's a stretch even by micro-optimization standards.
However, I can see it potentially masking other problems. Say you add LIMIT 1 to these queries, and then somehow the field upon which you're querying loses its unique index and the db gets multiple rows with the same value. This code will keep chugging along happily - I'd think you might want to fail fast instead, to become aware of (and fix) the bigger underlying problem.
In my own db interface code, I have a method queryOneRow() that runs some SQL and throws an exception if it gets more than one row back. It makes the most sense to me to handle this explicitly at the application layer rather than defensively in SQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With