tibberous Posted September 16, 2008 Share Posted September 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 16, 2008 Share Posted September 16, 2008 key=anything could be any number of results (depending) whereas unique_key=anything would, yes, be unique Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 16, 2008 Share Posted September 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 16, 2008 Share Posted September 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2008 Share Posted September 16, 2008 if you are expecting multiple results the query would be followed by a while loop. if you expect only a single row then it will be followed by a single fetch. I think an auditor would spot that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.