Go Back   SitePoint Forums > Forum Index > Program Your Site > Databases > MySQL
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Oct 18, 2009, 03:06   #1
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
is it necessary to use LIMIT 1?

Hi,

Suppose I have the following query:

MySQL Code:
SELECT * FROM `users` WHERE `user_name`='somename'

here the `user_name` field is primary key as well.

In the above, the user somename is retrieved through php. Anyway, my question is, if I know that there will only be one user of the same name - is it requrred to use LIMIT 1?

MySQL Code:
SELECT * FROM `users` WHERE `user_name`='somename' LIMIT 1

My brother says it's more efficient to do so, but I read somewhere that it may slow things down. But since it's just LIMIT 1 there shouldn't be much issue with speed. But I want to know what's the best practice.

Thanks
cssExp is offline   Reply With Quote
Old Oct 18, 2009, 05:22   #2
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
LIMIT 1 is ~not~ required if your WHERE clause guarantees only one row

best practice would be to leave it out

LIMIT is mysql syntax, and will not work if you ever have to port your code to a different database
r937 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:53.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved