SQL查询-组合

我有以下SQL查询。

SELECT *
FROM   contacts
WHERE  contact_id NOT IN (SELECT contact_id
                      FROM   campaign_extraction_history
                      WHERE  sf_oms_campaign_id = 'ABC-013505-2-2020'
                             AND sf_campaign_id = 'ABC-013505'
                             AND ( current_date - creation_date :: DATE ) <
                                 1
                             AND channel = 'BOTH'
                             AND ( Upper(status) = 'EXTRACTED'
                                    OR Upper(status) = 'LAUNCHED'
                                    OR Upper(status) = 'CONFIRMED' ))
   AND contact_id NOT IN (SELECT contact_id
                          FROM   campaign_extraction_history
                          WHERE  creation_date :: DATE = current_date
                                 AND channel = 'BOTH'
                                 AND ( Upper(status) = 'EXTRACTED'
                                        OR Upper(status) = 'LAUNCHED'
                                        OR Upper(status) = 'CONFIRMED' )
                          GROUP  BY contact_id
                          HAVING Count(*) > 10)
   AND contact_id NOT IN (SELECT contact_id
                          FROM   campaign_extraction_history
                          WHERE  sf_campaign_id = 'ABC-013505'
                                 AND channel = 'BOTH'
                                 AND ( Upper(status) = 'EXTRACTED'
                                        OR Upper(status) = 'LAUNCHED'
                                        OR Upper(status) = 'CONFIRMED' )
                          GROUP  BY contact_id
                          HAVING Count(*) >= 3)  

这需要很长时间才能执行。有没有办法重写它来优化它?

评论