Help optimizing this Query please

Discussion in 'MySQL' started by Tarek, Jan 8, 2011.

  1. #1
    Hello:
    i have this Query which is identified as a slow query.
    i don't have any more tricks to optimize it:

    ("SELECT usergroupid,avatarrevision,reputation,user.posts,user.usertitle,user.username,user.userid,COUNT(*) AS total FROM " . TABLE_PREFIX . "user AS user," . TABLE_PREFIX . "thread AS thread WHERE postuserid NOT IN ($notop) AND postuserid=thread.postuserid AND thread.dateline BETWEEN $sttopthpo AND $endtopthpo GROUP BY thread.postuserid ORDER BY total DESC LIMIT 1");


    here is it in the slow query log:
    # Query_time: 11 Lock_time: 0 Rows_sent: 1 Rows_examined: 64
    SELECT usergroupid,avatarrevision,reputation,user.posts,user.usertitle,user.username,user.userid,COUNT(*) AS total FROM user AS user,thread AS thread WHERE postuserid NOT IN (0) AND postuserid=thread.postuserid AND thread.dateline BETWEEN 1294444800 AND 1294531200 GROUP BY thread.postuserid ORDER BY total DESC LIMIT 1;



    i have indexed:
    user.userid
    thread.postuserid
    thread.dateline

    but still it is slow.

    any more thoughts?
     
    Last edited: Jan 8, 2011
    Tarek, Jan 8, 2011 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Did you try indexing user.postuserid?
     
    mwasif, Jan 8, 2011 IP
  3. Tarek

    Tarek Active Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    i indexed that, but i came up with another solution that i am working on now,
    i will add another table and store the info in it once daily and make the script to simply read from that table instead of running this query every time a visitor comes to the site.
     
    Tarek, Jan 8, 2011 IP