【发布时间】:2014-01-14 10:19:09
【问题描述】:
我有这个问题,这个线程中的讨论输出是什么:Mysql JOIN subquery
查看当前小提琴:http://sqlfiddle.com/#!2/e97cf/22
create table c_contact
(id INT,
name VARCHAR(20),
securityid INT
);
create table c_monitoring
(started DATE,
ended DATE DEFAULT NULL,
securityid INT
);
SELECT
c_contact.id,
c_contact.name,
c_contact.securityid
FROM c_contact
WHERE c_contact.securityid != ''
AND c_contact.securityid NOT IN
(select securityid
from c_monitoring
where ended is null
group by securityid
)
GROUP BY c_contact.id ;
我到底要如何优化这个查询?我在 c_contact 表中有 100.000 条记录,在 c_monitoring 表中有大约 10.000 条记录。 127 个结果行的查询耗时 > 30 秒。
编辑:通过正确索引表解决了案例。
【问题讨论】:
-
如果您解决了问题,请添加解决方案(使用的索引)作为答案。
标签: mysql optimization subquery notin