建议备用查询

时间:2016-06-27 10:39:55

标签: mysql performance

select  rcptto
    from  EmailDeliveryTracking edt
    where  edt.event in (2,3,4)
      and  edt.errordetail <> 9501
      and  not exists 
      ( SELECT  1
            from  EmailDeliveryTracking edt2
            where  edt.rcptto = edt2.rcptto
              and  edt2.event = 1
              and  edt2.eventtime >= date_add(now(),INTERVAL -6 month) 
      )
    group by  rcptto
    having  count(*) >= 3 ;

EmailDeliveryTracking有12101142条记录。

3 个答案:

答案 0 :(得分:0)

这是您的查询:

select rcptto
from EmailDeliveryTracking edt
where edt.event in (2, 3, 4) and
      edt.errordetail <> 9501 and
      not exists (select 1
                  from EmailDeliveryTracking edt2 
                  where edt.rcptto = edt2.rcptto and
                        edt2.event = 1 and
                        edt2.eventtime >= date_add(now(),INTERVAL -6 month
                 )
group by rcptto
having count(*) >= 3 ;

首先,你应该尝试索引。这有点棘手,开始的地方是EmailDeliveryTracking(rcptto, event, eventtime),也许是EmailDeliveryTracking(event, error_detail, rcptto)

答案 1 :(得分:0)

NOT EXISTS的替代方案是LEFT OUTER JOIN。性能明智哪一个最好将根据记录,索引等数量而变化

SELECT  edt.rcptto
FROM  EmailDeliveryTracking edt
LEFT OUTER JOIN  EmailDeliveryTracking edt2  ON edt.rcptto = edt2.rcptto
AND    edt2.event = 1
AND    edt2.eventtime >= DATE_ADD(NOW(),INTERVAL -6 MONTH)
WHERE  edt.event IN (2,3,4)
AND    edt.errordetail <> 9501
AND    edt2.rcptto IS NULL
GROUP BY  edt.rcptto
HAVING  COUNT(*) >= 3 ;

EmailDeliveryTracking 是否有 rcptto 的索引?

答案 2 :(得分:0)

只需按顺序添加INDEX(event, rcptto, eventtime)

event in (2,3,4)有多少行?有多少人errordetail <> 9501?如果这些答案相对较高(或较低),可能还有其他一些技术可以利用它。