分区是否会在内部联盟中被击中?

时间:2016-02-23 12:58:59

标签: sql oracle

我有以下SQL语句:

SELECT *
FROM (
    SELECT eu_dupcheck AS dupcheck
        , eu_date AS threshold
    FROM WF_EU_EVENT_UNPROCESSED
    WHERE eu_dupcheck IS NOT NULL
    UNION
    SELECT he_dupcheck AS dupcheck
        , he_date AS threshold
    FROM WF_HE_HISTORY_EVENT
    WHERE he_dupcheck IS NOT NULL
)
WHERE threshold > sysdate - 30

第二个表按日期分区,但第一个表不是。我需要知道第二个表的分区是否会在此查询中被命中,还是会进行全表扫描?

1 个答案:

答案 0 :(得分:0)

如果Oracle足够聪明以避免全表扫描,我会感到惊讶。请记住,UNION通过删除重复项来处理数据。因此,Oracle必须认识到:

  • where子句适用于分区(这实际上很简单)。
  • 该分区不会影响重复删除(这有点难,但是因为日期在select),所以是真的。

Oracle有一个智能优化器,所以也许它可以识别这种情况(它可能会避免UNION ALL的全表扫描)。但是,通过将条件移动到子查询,您会更安全:

SELECT *
FROM ((SELECT eu_dupcheck AS dupcheck, eu_date AS threshold
       FROM WF_EU_EVENT_UNPROCESSED
       WHERE eu_dupcheck IS NOT NULL AND eu_date > sysdate - 30
      ) UNION
      (SELECT he_dupcheck AS dupcheck, he_date AS threshold
       FROM WF_HE_HISTORY_EVENT
       WHERE he_dupcheck IS NOT NULL AND he_date > sysdate - 30
      )
     ) eh;