SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what's field should be INDEX in mysql?

    today, i checked my slow query log and saw lots of slow queries.

    it's looks like:

    SELECT uid,uname FROM usertbl WHERE country='us' ORDER BY lastlogin DESC LIMIT 0,20;

    i INDEXED country field.
    so should i index lastlogin field?

    and one more question, should i use DISTINCT or GROUP BY to filter duplicate records?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,275
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by phpguru View Post
    i INDEXED country field.
    so should i index lastlogin field?
    try it and see what happens

    Quote Originally Posted by phpguru View Post
    and one more question, should i use DISTINCT or GROUP BY to filter duplicate records?
    in general, yes

    but ~not~ in this case!

    if you are getting duplicates from the query that you posted, then you have some pretty serious issues
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Assuming uid is the primary key and uname is unique you should not have duplicates.
    The only code I hate more than my own is everyone else's.

  4. #4
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    if you are getting duplicates from the query that you posted, then you have some pretty serious issues
    i mean, DISTINCT and GROUP BY, which take more a lot of resources?

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,275
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by phpguru View Post
    i mean, DISTINCT and GROUP BY, which take more a lot of resources?
    it's not a question of resources, it's a question of why do you allow duplicates to occur in the first place?
    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
  •