优化Union All SQL

时间:2013-03-01 16:47:27

标签: sql oracle optimization

您能帮我优化以下Union All Query吗? 知道从哪里开始? 细节: SQL计划显示

| 7 |表访问完全| DELAY_DATE | 17533 | 171K | 25(0)| 00:00:01 |
| 8 |表访问完全|空缺| 25879 | 454K | 298(1)| 00:00:04 |

| 16 | MERGE加入笛卡尔| | 17533 | 273K | 28(4)| 00:00:01 |

这些潜在问题是什么?

 SELECT    delay_reason_desc, delay_start_date, delay_end_date, delay_reason_code
    FROM        delay_date dd
    LEFT JOIN   delay_reason dr
    ON          dr.delay_reason_id=dd.delay_reason_id
    LEFT JOIN   delay_interval di
    ON          di.delay_interval_id=dr.delay_interval_id
    LEFT JOIN   vacancy v
    ON          v.vacancy_id=dd.vacancy_id
    WHERE       dr.delay_reason_active_ind='Y'
    --AND         dd.vacancy_id=p_vacancy_id
    AND         delay_start_date >= vacancy_fpps_received_date
    AND         delay_end_date <= vacancy_open_date
    AND         delay_interval_code=1


UNION ALL
    SELECT    delay_reason_desc, delay_start_date, delay_end_date, delay_reason_code
    FROM        delay_date dd
    LEFT JOIN   delay_reason dr
    ON          dr.delay_reason_id=dd.delay_reason_id
    LEFT JOIN   delay_interval di
    ON          di.delay_interval_id=dr.delay_interval_id
    LEFT JOIN   vacancy v
    ON          v.vacancy_id=dd.vacancy_id
    WHERE       dr.delay_reason_active_ind='Y'
    --AND         dd.vacancy_id=p_vacancy_id
    AND         delay_start_date >= vacancy_open_date
    AND         delay_end_date <= vacancy_closed_date
    AND         delay_interval_code=2

2 个答案:

答案 0 :(得分:6)

这个怎么样 - 可能快两倍:

SELECT    delay_reason_desc, delay_start_date, delay_end_date, delay_reason_code
    FROM        delay_date dd
    LEFT JOIN   delay_reason dr
    ON          dr.delay_reason_id=dd.delay_reason_id
    LEFT JOIN   delay_interval di
    ON          di.delay_interval_id=dr.delay_interval_id
    LEFT JOIN   vacancy v
    ON          v.vacancy_id=dd.vacancy_id
    WHERE       dr.delay_reason_active_ind='Y'

    AND ((delay_start_date >= vacancy_fpps_received_date
         AND delay_end_date <= vacancy_open_date
         AND delay_interval_code=1)
    OR  (delay_start_date >= vacancy_open_date
          AND delay_end_date <= vacancy_closed_date
          AND delay_interval_code=2))

答案 1 :(得分:1)

create index delay_reason on delay_reason_id
create index delay_interval on delay_interval_id
create index vacancy on vacancy_id

create index delay_date on delay_reason_active_ind, delay_interval_code

然后调用@Hogan建议的SQL。