由于我的私人邮件数据库已经开始增长,因此我发现在以下查询中出现了一些相当缓慢的情况。使用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