查询改进运行缓慢

时间:2016-03-28 19:27:15

标签: mysql

我有一个查询,我不知道如何优化,但也许有人对此有所了解。

这是我的表:

      comments
        ---------
            +-------------+---------------+------+-----+---------+----------------+
            | Field       | Type          | Null | Key | Default | Extra          |
            +-------------+---------------+------+-----+---------+----------------+
            | commentid   | int(11)       | NO   | PRI | NULL    | auto_increment |
            | parentid    | int(11)       | YES  |     | 0       |                |
            | refno       | int(11)       | YES  |     | 0       |                |
            | createdate  | int(11)       | YES  | MUL | 0       |                |
            | remoteip    | varchar(80)   | YES  |     |         |                |
            | fingerprint | varchar(50)   | YES  |     |         |                |
            | locid       | int(11)       | YES  | MUL | 0       |                |
            | clubid      | int(11)       | YES  |     | 0       |                |
            | profileid   | int(11)       | YES  | MUL | 0       |                |
            | userid      | int(11)       | YES  | MUL | 0       |                |
            | global      | int(11)       | YES  |     | 0       |                |
            | official    | int(11)       | YES  |     | 0       |                |
            | legacyuser  | int(11)       | YES  | MUL | 0       |                |
            | mediaid     | int(11)       | YES  |     | 0       |                |
            | status      | int(11)       | YES  |     | 1       |                |
            | comment     | varchar(4000) | YES  |     |         |                |
            | likes       | int(11)       | YES  |     | 0       |                |
            | dislikes    | int(11)       | YES  |     | 0       |                |
            | import      | int(11)       | YES  |     | 0       |                |
            | author      | varchar(50)   | YES  |     |         |                |
            +-------------+---------------+------+-----+---------+----------------+

这是我的表格解释:

        | comments | CREATE TABLE `comments` (
      `commentid` int(11) NOT NULL AUTO_INCREMENT,
      `parentid` int(11) DEFAULT '0',
      `refno` int(11) DEFAULT '0',
      `createdate` int(11) DEFAULT '0',
      `remoteip` varchar(80) DEFAULT '',
      `fingerprint` varchar(50) DEFAULT '',
      `locid` int(11) DEFAULT '0',
      `clubid` int(11) DEFAULT '0',
      `profileid` int(11) DEFAULT '0',
      `userid` int(11) DEFAULT '0',
      `global` int(11) DEFAULT '0',
      `official` int(11) DEFAULT '0',
      `legacyuser` int(11) DEFAULT '0',
      `mediaid` int(11) DEFAULT '0',
      `status` int(11) DEFAULT '1',
      `comment` varchar(4000) DEFAULT '',
      `likes` int(11) DEFAULT '0',
      `dislikes` int(11) DEFAULT '0',
      `import` int(11) DEFAULT '0',
      `author` varchar(50) DEFAULT '',
      PRIMARY KEY (`commentid`),
      KEY `comments_locid` (`locid`),
      KEY `comments_userid` (`userid`),
      KEY `idx_legacyusers` (`legacyuser`),
      KEY `profile_index` (`profileid`),
      KEY `comments_createdate` (`createdate`),
      KEY `compound_for_comments` (`locid`,`global`,`status`),
      KEY `global` (`global`),
      KEY `status` (`status`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3848451 DEFAULT CHARSET=latin1

最后,这是我的疑问:

SELECT c.createdate commentdate 
  FROM comments c
 WHERE status > 0 
   AND locid 
    IN 
     ( SELECT locid 
         FROM locations 
        WHERE state = 'NJ'
     ) 
 ORDER 
    BY c.createdate DESC 
 LIMIT 15

子查询的locid和state上有索引

正如您所看到的,针对位置表有一个子选择,用于表示要用于外部查询的locids

希望有一些改进空间。

2 个答案:

答案 0 :(得分:0)

这可能相当,而且可能更快一点。

select *,comments.createdate as commentdate 
from comments FORCE INDEX (locid_status)
inner join locations FORCE INDEX (your_index_name) on (comments.locid  = locations.locid and locations.state='NJ')
where comments.status>0 
order by comments.createdate desc limit 15

答案 1 :(得分:0)

如果locations是一个小表连接,它直接比使用子查询更快。

select *,comments.createdate as commentdate 
from comments as c, locations as l 
where status > 0 
and l.state = 'NJ'
and l.locid = c.locid
order by c.createdate desc limit 15;