2011-10-19 46 views
0

我使用mysql作为消息传递系统。我正在尝试编写一个查询,以便将用户与之通信的每个其他用户发送或接收的最后一条消息提取出来。有JOIN,UNION和ORDER BY的子查询,使用临时文件使用filesort

这是存放所有的邮件表:

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) 

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

+0

请问您的查询甚至给出正确的结果? –

+0

是的查询返回正确的结果 – Brian

回答

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 
相关问题