我有一个简单的邀请表:
CREATE TABLE `invitation` (
`invitation_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inviter_id` int(10) unsigned NOT NULL,
`invitee_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`invitation_id`),
UNIQUE KEY `invitee_inviter_idx` (`invitee_id`,`inviter_id`)
)
我想选择邀请人 70 对被邀请人 62 的邀请,反之亦然:
EXPLAIN SELECT * FROM `invitation` WHERE
(invitee_id = 70 AND inviter_id = 62) OR (invitee_id = 62 AND inviter_id = 70)
但是这个查询是 ALL 类型的,并且不使用受邀者_邀请者_idx。请告诉我这里有什么问题?
谢谢!
==EDIT== 抱歉,我对架构有误,它还有一个字段:request_ts。这次查询计划是 ALL。
CREATE TABLE `invitation` (
`invitation_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inviter_id` int(10) unsigned NOT NULL,
`invitee_id` int(10) unsigned NOT NULL,
`request_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`invitation_id`),
UNIQUE KEY `invitee_inviter_idx` (`invitee_id`,`inviter_id`)
)
这是我的解释结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE invitation ALL invitee_inviter_idx \N \N \N 1 Using where