由於我的私人郵件數據庫已經開始增長,因此我發現在以下查詢中出現了一些相當緩慢的情況。使用OR運算符時,MySQL查詢速度減慢
查詢:
SELECT * FROM privatemessages WHERE sender='940' OR recipient='940' ORDER BY id DESC LIMIT 1000;
(940可以是任何用戶ID)
表:
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=8650153 DEFAULT CHARSET=latin1
MySQL的說明:
+----+-------------+-----------------+-------------+------------------------------------------------------------------+------------------+---------+------+-------+--------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+-------------+------------------------------------------------------------------+------------------+---------+------+-------+--------------------------------------------+
| 1 | SIMPLE | privatemessages | index_merge | recipient,sender,openmessagingpanel,openpmthreadrev,openpmthread | sender,recipient | 4,4 | NULL | 26100 | Using union(sender,recipient); Using where |
+----+-------------+-----------------+-------------+------------------------------------------------------------------+------------------+---------+------+-------+--------------------------------------------+
1 row in set (0.00 sec)
有誰知道我需要做什麼才能使此查詢備份速度?大約有800萬條記錄。
謝謝。
你真的需要爲你找到的行提取所有的字段('SELECT *')嗎? – 2011-04-26 07:38:38
您是否爲發件人和收件人組合了此表上的另一個索引?當我把你的餐桌定義,我得到一個更清潔的解釋計劃: – Wes 2011-04-26 07:39:15
對不起,我搞亂了我的副本和粘貼。原來的帖子已經被數據庫現在的版本糾正了,儘管它今天早些時候測試了一些東西很麻煩。 – Brian 2011-04-26 07:53:54