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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.