1

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

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

| 16 | 合并加入笛卡尔| | 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
4

2 回答 2

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))
于 2013-03-01T16:50:36.010 回答
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。

于 2013-03-01T16:54:30.277 回答