2011-04-26 87 views
1

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

谢谢。

+0

你真的需要为你找到的行提取所有的字段('SELECT *')吗? – 2011-04-26 07:38:38

+0

您是否为发件人和收件人组合了此表上的另一个索引?当我把你的餐桌定义,我得到一个更清洁的解释计划: – Wes 2011-04-26 07:39:15

+0

对不起,我搞乱了我的副本和粘贴。原来的帖子已经被数据库现在的版本纠正了,尽管它今天早些时候测试了一些东西很麻烦。 – Brian 2011-04-26 07:53:54

回答

0

2想法:

  • 是否值得创建与未删除的状态消息的看法,并从那里选择的消息,在假设就会有较少的伦理委员会来处理。
  • 作为冗长约SELECT-ING您需要的字段通常会带来性能增益
0

你媒体链接对发送者和接收者列的索引。

假设你需要对GUI的要求,用户可以选择邮件阅读:

如果你只选择ID,标题,也许是这个时间应该够显示的链接的消息。也可以将限制设置为50左右,并进行一些排版。

或者您是否需要某种出口?那么你应该使用mysql的导出功能...

希望这可以帮助你。