Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Performance - Select one row, should I use limit 1? [duplicate]

I'm trying to find out if using "limit 1" would work faster while executing a "select" query on MySQL database with milions of records, like:

SELECT * FROM users where id = 99999;

SELECT * FROM users where id = 99999 LIMIT 1; /* Is it faster? */

I made some tests executing some queries with and without the LIMIT, but the difference between them are really small (using LIMIt has some better speed tho), but I'm not sure if there is any difference between using the "WHERE" in a not indexed column and in an indexed column, if it would have some performance problem or not.

Should I use the "LIMIT 1" in all queries that I want to return only one result even knowing that the "WHERE id = 99999" will return only one row?

like image 315
Imac Avatar asked Dec 13 '25 15:12

Imac


1 Answers

In addition to the question of the performance, I would always use LIMIT in this case. Reason: the quality and readability of the source code increases, since reading the code already determines that only one return value is expected. If it is a column that is not unique, the LIMIT statement is, in my opinion, mandatory to avoid errors by returning multiple values.

like image 57
Datendenker Avatar answered Dec 16 '25 16:12

Datendenker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!