Skip to content
Advertisement

Does using “LIMIT 1” speed up a query on a primary key?

If I have a primary key of say id and I do a simple query for the key such as,

SELECT id FROM myTable WHERE id = X

Will it find one row and then stop looking as it is a primary key, or would it be better to tell mysql to limit its select by using LIMIT 1? For instance:

SELECT id FROM myTable WHERE id = X LIMIT 1

Advertisement

Answer

Does using “LIMIT 1” speed up a query on a primary key?

No. It’s already as fast as can be without LIMIT 1. LIMIT 1 is effectively implied anyway.

Will it find one row and then stop looking as it is a primary key

Yes.

No table scan should be necessary at all here: it’s a key-based lookup. The matching row is found and that’s the end of the procedure.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement