Please Help me Optimize This MYSQL Query!

Discussion in 'MySQL' started by min0taur, Jul 27, 2009.

  1. #1
    Is there any other way to optimize this query

    SELECT name
    FROM `users`
    ORDER BY id
    DESC LIMIT 0 , 100
    PHP:
    I want to display the last 100 users. Now im wondering if there is a way to optimize it because in explain it seek all rows in users table and too slow in large table thanks. By the way 'id' is primary index
     
    min0taur, Jul 27, 2009 IP
  2. ITchimes123

    ITchimes123 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think there is no other way to optimize this query.
     
    ITchimes123, Jul 27, 2009 IP
  3. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Assuming the ID is a serial or sequence

    Select max(id) from users
    SELECT name from users where id >maxid-100 order by id.
    But this will not work for most ACID compliant databases since inserts can be rolled back, for Mysql with MyIsam this might work.

    An alternative is to use a hint column to decrease the amount of work Mysql needs to perform.
    1)create a column last100users_hint for each new user this needs to be true. create an index on this column.
    2) Update the last100users_hint column every 24 hours by marking only the last 100 ids true using an update version of the slow query in your first post.
    3) When user needs the last 100 list, then perform the query using your first query plus an AND last100users_hint=true.

    The query planner should use you hint column to discard most records, the remaining records will then be used in your first query.
    So the hints column will reduce your millions of users => last 100+users created in the last 24 hours.
    Your original query will reduce the last 100+users created in the last 24 hours=>last 100 users.
     
    chisara, Aug 10, 2009 IP
  4. WelcomeKarizma

    WelcomeKarizma Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The query planner should use you hint column to discard most records, the remaining records will then be used in your first query.
     
    WelcomeKarizma, Aug 11, 2009 IP
  5. jjwdesign

    jjwdesign Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You say it is slow. Maybe it's the type of table and MySQL configuration you are using that is causing the problem. You might wish to look into INNODB MySQL table types and configure MySQL (ini) to use them for your needs.
     
    jjwdesign, Aug 11, 2009 IP
  6. Somebodysomeone

    Somebodysomeone Peon

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    how much is too slow lol?
    i think there is nothing to do for this q and it should be pretty fast.
     
    Somebodysomeone, Aug 12, 2009 IP
  7. elitasson

    elitasson Member

    Messages:
    49
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #7
    Hehe, can't make it more optimized then what it is right now, sorry. Maybe take a lock at the hardware and make sure that the server is not overloaded.
     
    elitasson, Aug 13, 2009 IP