Jump to content

limit 1 worth saying?


tibberous

Recommended Posts

If I am trying to get a single record from a database, by it's unique key, is there any reason to specify 'limit 1'? Now that I think about it, if the key is unique, then saying where key=anything should infer limit 1, an optimization that I would think mysql's designers would not have overlooked.

Link to comment
https://forums.phpfreaks.com/topic/124535-limit-1-worth-saying/
Share on other sites

Adding "LIMIT 1" has additional benefit of telling anyone who reads your code, that the query in question will return no more than 1 row. They don't have to know if key is unique or not for that.

 

This is true

 

However,

 

Run an EXPLAIN query with the LIMIT attached and witout and examine what MySQL is doing.  Most of the time you can learn right from it if it is a good or bad thing.

 

For the most part I do not LIMIT 1 on querying a single PK.  However as stated it could e a FK and thus an auditer might not see it being a multi row query

Link to comment
https://forums.phpfreaks.com/topic/124535-limit-1-worth-saying/#findComment-643184
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.