sql - Mysql NOT IN运算符对于大结果集的性能问题?

标签 sql mysql performance

我有以下两个问题:

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_codewpi.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/

相关文章:

php - 如何从两个表中查询第三个表的条件

mysql - 当两个表字段在 MySQL 中具有相同的值时,如何忽略将记录从一个表插入另一个表?

mysql - 如何在 MYSQL 中更新/使 View 可更新

java - 有效识别请求参数的方法

sql - 使用动态sql删除表中的多个表

sql - “some”函数是通用SQL的一部分吗?

php - 如何在单个 php 准备语句上同时使用 SELECT 和 INSERT 查询?

php - 如何通过中间表在从一个模型到另一个模型中使用 Eloquent

objective-c - 哪个代码块是 'better' ?

c++ - 在 clock_gettime() 中使用 _COARSE 变体仍然调用 sys_clock_gettime() 系统调用