为什么这个查询在 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;
编辑:添加了 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 |
我已经在收件人信息表上添加了索引,但我不知道我应该在消息表上索引什么以使其使用键。我猜从其他信息中我发现我应该使用复合索引,但是使用哪个表的哪些列?“使用临时”是可以避免的吗?任何提示或帮助表示赞赏:)