哪些索引会加速这个运行缓慢的查询?

时间:2013-02-24 19:04:03

标签: mysql sql query-optimization

为什么这个查询在MySQL 5.5.30上运行得很慢?:


SELECT message.mid,
       message.sender,
       message.date,
       message.subject,
       message.body,
       message.folder,
       sender.firstname,
       sender.lastname,
       sender.status,
       Group_concat(DISTINCT recipientinfo.rvalue) AS recipientAddresses,
       Group_concat(DISTINCT recipients.lastname)  AS recipientLastNames,
       Group_concat(recipients.status)             AS recipientPositions
FROM   message
       LEFT JOIN employeelist AS sender
              ON message.sender = sender.email_id
       LEFT JOIN recipientinfo
              ON message.mid = recipientinfo.mid
       LEFT JOIN employeelist AS recipients
              ON recipientinfo.rvalue = recipients.email_id
GROUP  BY recipientinfo.mid
ORDER  BY NULL; 

Results of EXPLAIN

编辑:添加了CREATE TABLE输出:

| message | CREATE TABLE `message` (
  `mid` int(10) NOT NULL DEFAULT '0',
  `sender` varchar(127) NOT NULL DEFAULT '',
  `date` datetime DEFAULT NULL,
  `message_id` varchar(127) DEFAULT NULL,
  `subject` text,
  `body` text,
  `folder` varchar(127) NOT NULL DEFAULT '',
  PRIMARY KEY (`mid`),
  KEY `sender` (`sender`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |

| employeelist | CREATE TABLE `employeelist` (
  `eid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `firstName` varchar(31) NOT NULL DEFAULT '',
  `lastName` varchar(31) NOT NULL DEFAULT '',
  `Email_id` varchar(31) NOT NULL DEFAULT '',
  `Email2` varchar(31) DEFAULT NULL,
  `Email3` varchar(31) DEFAULT NULL,
  `EMail4` varchar(31) DEFAULT NULL,
  `folder` varchar(31) NOT NULL DEFAULT '',
  `status` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`eid`),
  UNIQUE KEY `Email_id` (`Email_id`),
  KEY `firstName` (`firstName`),
  KEY `lastName` (`lastName`),
  KEY `status` (`status`),
  KEY `Email2` (`Email2`,`Email3`,`EMail4`)
) ENGINE=MyISAM AUTO_INCREMENT=153 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC |

| recipientinfo | CREATE TABLE `recipientinfo` (
  `rid` int(10) NOT NULL DEFAULT '0',
  `mid` int(10) unsigned NOT NULL DEFAULT '0',
  `rtype` enum('TO','CC','BCC') DEFAULT NULL,
  `rvalue` varchar(127) DEFAULT NULL,
  `dater` datetime DEFAULT NULL,
  PRIMARY KEY (`rid`),
  KEY `rvalue` (`rvalue`),
  KEY `mid` (`mid`),
  KEY `rtype` (`rtype`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |

我在recipientinfo表上添加了索引,但我不知道应该在消息表上索引什么以使其使用密钥。我想从其他信息我发现我应该使用复合索引,但使用哪个表的哪些列? “使用临时”是否可以避免?任何提示或帮助表示赞赏:)

0 个答案:

没有答案