使用JOIN,UNION和ORDER BY的子查询,使用临时,使用filesort

时间:2011-10-19 23:51:17

标签: mysql

我正在使用mysql作为消息传递系统。我正在尝试编写一个查询,该查询将提取用户与之通信的每个其他用户发送或接收的最后一条消息。

这是包含所有消息的表:

CREATE TABLE `privatemessages` (
  `id` int(11) NOT NULL auto_increment,
  `recipient` int(11) NOT NULL,
  `sender` int(11) NOT NULL,
  `time` int(11) NOT NULL,
  `readstatus` int(11) NOT NULL,
  `message` varchar(255) NOT NULL,
  `messagetype` int(11) NOT NULL,
  `rdeleted` int(11) NOT NULL,
  `sdeleted` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `recipient` (`recipient`),
  KEY `sender` (`sender`),
  KEY `read` (`readstatus`),
  KEY `time` (`time`),
  KEY `openmessagingpanel` (`recipient`,`readstatus`),
  KEY `openpmthreadrev` (`recipient`,`sender`),
  KEY `openpmthread` (`sender`,`recipient`)
) ENGINE=InnoDB AUTO_INCREMENT=27587533 DEFAULT CHARSET=latin1$$

以下是给我提问的查询:

select * from 
(
select users.username, users.onlinestatus,users.profileimageid, privatemessages.id, privatemessages.time, privatemessages.message from privatemessages 
JOIN users on privatemessages.recipient=users.id WHERE sender=19 
UNION ALL 
select users.username, users.onlinestatus,users.profileimageid, privatemessages.id, privatemessages.time, privatemessages.message from privatemessages 
JOIN users on privatemessages.sender=users.id WHERE recipient=19 
ORDER BY id DESC
)
as testResult GROUP by testResult.username ORDER By id DESC;

查询以正确的顺序返回我想要的数据,但对于有大量消息的用户来说,这是非常慢的。

以下是解释声明:

+----+--------------+-----------------+--------+------------------------------------------------------------------+--------------------+---------+---------------------------------+------+---------------------------------+
| id | select_type  | table           | type   | possible_keys                                                    | key                | key_len | ref                             | rows | Extra                           |
+----+--------------+-----------------+--------+------------------------------------------------------------------+--------------------+---------+---------------------------------+------+---------------------------------+
|  1 | PRIMARY      | <derived2>      | ALL    | NULL                                                             | NULL               | NULL    | NULL                            | 4246 | Using temporary; Using filesort |
|  2 | DERIVED      | privatemessages | ref    | recipient,sender,openmessagingpanel,openpmthreadrev,openpmthread | sender             | 4       |                                 | 1076 |                                 |
|  2 | DERIVED      | users           | eq_ref | PRIMARY                                                          | PRIMARY            | 4       | chat2.privatemessages.recipient |    1 |                                 |
|  3 | UNION        | privatemessages | ref    | recipient,sender,openmessagingpanel,openpmthreadrev,openpmthread | openmessagingpanel | 4       |                                 | 6490 |                                 |
|  3 | UNION        | users           | eq_ref | PRIMARY                                                          | PRIMARY            | 4       | chat2.privatemessages.sender    |    1 |                                 |
| NULL | UNION RESULT | <union2,3>      | ALL    | NULL                                                             | NULL               | NULL    | NULL                            | NULL | Using filesort                  |
+----+--------------+-----------------+--------+------------------------------------------------------------------+--------------------+---------+---------------------------------+------+---------------------------------+
6 rows in set (0.08 sec)

我应该使用更好的查询吗? 感谢

1 个答案:

答案 0 :(得分:1)

尝试此查询

SELECT users.username, users.onlinestatus, users.profileimageid, temp.id, temp.time, temp.message
  FROM users
 INNER JOIN (SELECT id, time, message, recipient FROM privatemessages WHERE sender=19) temp
    ON temp.recipient=users.id
 UNION 
SELECT users.username, users.onlinestatus,users.profileimageid, temp.id, temp.time, temp.message
  FROM users
 INNER JOIN (SELECT id, time, message, sender FROM privatemessages WHERE sender=19) temp
    ON temp.sender=users.id
 GROUP BY username ORDER BY id DESC