我有以下两个问题:
select count(*)
from segmentation_cycle_recipients scr
, segmentation_instance si
where si.access_code=scr.access_code
and si.segment_id is NOT NULL;
在 0.2 秒内返回 13429 行
2)
select count(*)
from segmentation_cycle_recipients scr
, segmentation_instance si, web_pat_info wpi
where si.access_code=scr.access_code and scr.siebel_row_id=wpi.siebel_id
and si.segment_id is NOT NULL;
在 0.48 秒内返回 4003 行
现在,我想要 1)-2) 所以我编写了以下查询:
select count(*)
from segmentation_cycle_recipients scr
, segmentation_instance si
where si.access_code=scr.access_code
and si.segment_id is NOT NULL
and scr.siebel_row_id NOT IN (select scr.siebel_row_id
from segmentation_cycle_recipients scr
, segmentation_instance si
, web_pat_info wpi where si.access_code=scr.access_code
and scr.siebel_row_id=wpi.siebel_id and si.segment_id is NOT NULL);
我期待 13429-4003=9426 行,但查询需要永远执行(必须终止查询命令)。它甚至在 mysql>status;) 的“慢查询”列表中添加了一个计数器
它在结果集小得多的开发环境中返回 < 100 毫秒。所以我相信查询本身是正确的。
我相信,使用 NOT IN 是 Mysql 中的一个已知性能问题(Oracle 有 MINUS 运算符)。关于如何提高此查询的性能的任何建议?
最佳答案
SELECT COUNT(*)
FROM segmentation_cycle_recipients scr
JOIN segmentation_instancs si
ON si.access_code = scr.access_code
LEFT JOIN
web_pat_info wpi
ON wpi.siebel_id = scr.siebel_row_id
WHERE wpi.siebel_id IS NULL
AND si.segment_id is NOT NULL
确保 si.access_code
和 wpi.siebel_id
被索引,并且 wpi.siebel_id
被定义为 NOT NULL
.
如果后一个条件不成立,将 WHERE
子句中的 wpi.siebel_id IS NULL
替换为定义为 NOT NULL
的任何其他列.
关于sql - Mysql NOT IN运算符对于大结果集的性能问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1702353/