我正在尝试进行查询以查找特定用户的电子邮件,该用户要么编写电子邮件(存储在电子邮件表本身中),要么在电子邮件中抄送,存储在另一封电子邮件中,该电子邮件已加入到电子邮件表中查询。
这个查询很慢。email_cc 表中有大约 150 万封电子邮件和令牌数量的条目;但是 WHERE 子句中的 OR 会将针对电子邮件表的简单查询变成需要 1,000 倍的查询。
如何重写此查询以使其高效?
**email**
id (PRIMARY)
userid (INDEX)
**email_cc**
id (PRIMARY)
userid (INDEX)
emailid (INDEX)
SELECT email.id, email.userid
FROM email
LEFT JOIN email_cc ON (email_cc.emailid = email.id)
WHERE (email.userid = 5 OR email_cc.userid = 5)
回应评论:运行此查询:
EXPLAIN SELECT COUNT(DISTINCT email.id) FROM email
LEFT JOIN email_cc ON email_cc.emailid = email.id
WHERE (email.userid = 468741 OR email_cc.userid = 468741)
获取
SIMPLE email index IDX_54469DF4217BBB47 IDX_54469DF4217BBB47 5 NULL 1514520 Using index
SIMPLE email_cc ref IDX_8D675752700047D2 IDX_8D675752700047D2 4 email.id 1 Using where