2011-10-19 79 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 
相關問題