SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is it necessary to use LIMIT 1?

    Hi,

    Suppose I have the following query:

    Code MySQL:
    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?

    Code MySQL:
    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

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •