调整具有NOT EXIST子句的Oracle SQL

时间:2014-11-26 11:20:39

标签: oracle11g database-performance plsqldeveloper sql-tuning

我想调整下面的查询,消除其中指定的NOT EXIST子句。你能帮忙吗?

GLT_temp_upload是临时表,其中DA_DUEDATE是包含大量数据的分区表。

请帮忙

SELECT  DISTINCT
              batchid,
              store_area,
                 STORE_AREA
              || ','
              || STORE_ID
              || ','
              || SMS_ID
              || ','
              || SMS_SERVICE
              || ','
              || SYNERGY_MODE_ID
              || ','
              || FREQUENCY
              || ','
              || DUEDATE
              || ','
              || STUDY_ID
              || ','
              || YEAR
              || ''
              || WEEK_ID
              ||',Not exist in Da_Duedate'
         FROM GLT_temp_upload upload
        WHERE     upload.batchid = 1
              AND NOT EXISTS
                         (SELECT due.week_id,
                                 due.country_id,
                                 due.year,
                                 due.study_id,
                                 due.store_id,
                                 due.store_area,
                                 due.synergy_mode_id,
                                 upload.batchid,
                                 due.due_date,
                                 upload.sms_service
                            FROM DA_DUEDATE due
                           WHERE     due.store_id = upload.store_id
                                 AND due.study_id = upload.study_id
                                 AND due.store_area = upload.store_area
                                 AND due.frequency = upload.frequency
                                 AND due.sms_service = upload.sms_service
                                 AND due.week_id = upload.week_id
                                 AND due.country_id = upload.country_id
                                 AND due.year = upload.year
                                 AND due.sms_id = upload.sms_id
                                 AND due.synergy_mode_id =
                                        upload.synergy_mode_id)

1 个答案:

答案 0 :(得分:0)

您可以尝试NOT EXISTS / LEFT JOIN / NOT IN

在您的NOT EXISTS中,SELECT 1代替列表

就足够了

有时LEFT JOIN可能更有用(取决于索引,表的大小等)

SELECT  DISTINCT
              batchid,
              store_area,
                 STORE_AREA
              || ','
              || STORE_ID
              || ','
              || SMS_ID
              || ','
              || SMS_SERVICE
              || ','
              || SYNERGY_MODE_ID
              || ','
              || FREQUENCY
              || ','
              || DUEDATE
              || ','
              || STUDY_ID
              || ','
              || YEAR
              || ''
              || WEEK_ID
              ||',Not exist in Da_Duedate'
         FROM GLT_temp_upload upload left join DA_DUEDATE due
              ON due.store_id = upload.store_id
                 AND due.study_id = upload.study_id
                 AND due.store_area = upload.store_area
                 AND due.frequency = upload.frequency
                 AND due.sms_service = upload.sms_service
                 AND due.week_id = upload.week_id
                 AND due.country_id = upload.country_id
                 AND due.year = upload.year
                 AND due.sms_id = upload.sms_id
                 AND due.synergy_mode_id = upload.synergy_mode_id
        WHERE upload.batchid = 1 and due.store_id is NULL;

我建议您查看执行计划,为您的案例找到最佳解决方案。