Search code examples
mysqldatabaserowmysql5

best way for select row mysql


Which of the following notations is better?

SELECT id,name,data FROM table WHERE id = X

OR

SELECT id,name,data FROM table WHERE id = X LIMIT 1

I think it should not have "LIMIT".

Thanks!


Solution

  • If there is a unique constraint on id then they will be exactly the same.

    If there isn't a unique constraint (which I would find highly surprising on a column called id) then which is better depends on what you want to do:

    • If you want to find all rows matching the condition, don't use LIMIT.
    • If you want to find any row matching the condition (and you don't care which), use LIMIT 1.