0

Let's say a table has an ID column, PRIMARY KEY with AUTOINCREMENT.

If I do a query like:

SELECT * FROM table WHERE ID = ?

is it necessary to add LIMIT 1 ?

Would that increase performance or something?

I'm using SQLite with PHP's PDO.

thelolcat
  • 10,995
  • 21
  • 60
  • 102

1 Answers1

5

If it is a primary key, then the engine should do the right thing. A primary key index is a unique index, so MySQL knows there is only one matching value.

But even if it doesn't, the impact on performance would be exceedingly minor. The engine would use the primary key index to find the right value, going directly to the right place in the index. It will then load the data page (in order to satisfy the select *).

Then, either it stops right away. Or, it scans and reads the next value in the index, and stops. The additional overhead would be reading the next value in the index. A very minor overhead compared to loading the data page.

If you are interested in such things, you should check out the documentation.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786