Indexing queries

Discussion in 'MySQL' started by powerbfore, Jul 24, 2012.

  1. #1
    Hi, I recently noticed my site loading slow and mysqld process using lots of CPU usage. And I decided that I should fix slow queries by indexing.
    However, I'm not exactly sure how I can understand completely what table.

    This is one(?) example of a slow query I have in my db
    # Query_time: 3  Lock_time: 0  Rows_sent: 20  Rows_examined: 22491SELECT
                                    user.avatarid,
                                    user.avatarrevision,
                                    user.username,
                                    user.usergroupid,
                                    user.membergroupids,
                                    user.infractiongroupid,
                                    user.displaygroupid,
                                    user.dbtech_vbshout_settings AS shoutsettings,
                                    user.dbtech_vbshout_shoutstyle AS shoutstyle,
                                    vbshout.*
                                    , avatar.avatarpath, NOT ISNULL(customavatar.userid) AS hascustomavatar, customavatar.dateline AS avatardateline, customavatar.width AS avwidth, customavatar.height AS avheight, customavatar.height_thumb AS avheight_thumb, customavatar.width_thumb AS avwidth_thumb, customavatar.filedata_thumb,
                                    pmuser.username AS pmusername
    
    
                            FROM dbtech_vbshout_shout AS vbshout
                            LEFT JOIN user AS user ON(user.userid = vbshout.userid)
    
    
                            LEFT JOIN avatar AS avatar ON (avatar.avatarid = user.avatarid)
                            LEFT JOIN customavatar AS customavatar ON (customavatar.userid = user.userid)
    
    
                            LEFT JOIN user AS pmuser ON(pmuser.userid = vbshout.id)
    
    
                            WHERE vbshout.instanceid IN(-1, 0, 1)
                                    AND vbshout.userid NOT IN(
                                            SELECT ignoreuserid
                                            FROM dbtech_vbshout_ignorelist AS ignorelist
                                            WHERE userid = 2665
                                    )
                                    AND vbshout.forumid IN(5,6,7,73,75,10,11,83,81,82,18,19,20,53,22,23,24,25,26,27,28,29,30,31,32,33,34,35,86,36,37,38,39,55,52,58,57,59,60,68,69,54,79,41,42,77,43,44,45,80,46,47,76,48,49,50,0)
                                    AND (
                                            user.dbtech_vbshout_banned = '0' OR
                                            vbshout.userid = -1
                                    )
                                    AND (
                                    vbshout.userid IN(-1, 2665) OR
                                    vbshout.id IN(0, 2665)
                            )AND vbshout.type NOT IN(64,128) AND vbshout.chatroomid = 0 AND vbshout.userid NOT IN(
            SELECT userid
            FROM user AS user
            WHERE dbtech_vbshout_silenced = 1
                    AND userid != 2665
    )
                            ORDER BY dateline DESC
                            LIMIT 20;
    
    
    
    Code (markup):
    I'm trying to use this: http://parand.com/say/index.php/2009/09/01/finding-and-fixing-slow-mysql-queries/
    but when I type the command "explain ________" I don't get any response, leading me to think that I typed the wrong table.
    So I'm wondering, what is it do I type?

    Edit: oh and if the slow query is the query time, which in here is 3, it goes much higher during the day, I believe.
     
    Last edited: Jul 24, 2012
    powerbfore, Jul 24, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Your nested queries are going to add a huge amount of overhead.

    vbshout.userid NOT IN(
    SELECT ignoreuserid

    and

    vbshout.userid NOT IN(
    SELECT

    These basically increase the overhead exponentially. This isn't about a table, it's most likely about how complex the query is and your server is hardware and configuration limited to execute this. Get rid of the nested queries if at all possible. INNER JOIN is a good way of doing it.
     
    jestep, Jul 24, 2012 IP