Is "LIMIT 1" recommended for query where WHERE condition is based on PK?

limit, mysql

Solution

I would call that a bad practice as when it comes to something like a `userid` it's generally unique and you won't have more than one. Therefore, having `LIMIT 1` seems pretty contradictory and someone who comes to maintain your code later may have to second-guess your design.

Also, I don't think it has any speed benefit at all. You can check out mySQL's Explain for a simple tool to analyze a query.

Note, as mentioned in the comments. `LIMIT #` does have speed and general benefits in other cases, just not this one.

Problem

I am querying a mySQL database to retrieve the data from 1 particular row. I'm using the table primary key as the WHERE constraint parameter. E.g. ``` SELECT name FROM users WHERE userid = 4 ``` The userid column is the primary key of the table. Is it good practice to use LIMIT 1 on the end of that mySQL statement? Or are there any speed benefits?

Original source