任何人都可以看看如何优化此查询。相同的复杂查询多次合并,并且UNION ALL的“最后更新日期”条件在每个集合中都不相同。 我可以通过添加一个条件而不重复相同的查询来缩短下面的查询,这将给出相同的结果但是查询的运行时间长了10倍
(
AND dist_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
OR line_locations_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
OR line_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
OR head_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
OR releases_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS') )
查询如下:
SELECT
*
FROM
table 1.. table 8
WHERE
dist_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
/*all join conditions here */
UNION ALL
SELECT
*
FROM
table 1.. table 8
WHERE
dist_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_locations_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
/*all join conditions here */
UNION ALL
SELECT
*
FROM
table 1.. table 8
WHERE
dist_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_locations_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
/*all join conditions here */
UNION ALL
SELECT
*
FROM
table 1.. table 8
WHERE
dist_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_locations_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND head_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
/*all join conditions here */
SELECT
*
FROM
table 1.. table 8
WHERE
dist_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_locations_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND line_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND head_all.last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND releases_all.last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
/*all join conditions here */
答案 0 :(得分:0)
尝试一下
WITH DTL AS
(
SELECT da.last_update_date as da_last_update_date,
lla.last_update_date as lla_last_update_date,
line_all.last_update_date as la_last_update_date,
head_all.last_update_date as ha_last_update_date,
releases_all.last_update_date as ra_last_update_date,
.......
FROM head_all ha
INNER JOIN dist_all da on (ha.header_id = da.header_id)
INNER JOIN line_all la on (da.line_id = la.line_id )
INNER JOIN line_locations_all lla on (da.line_location_id = lla.line_location_id )
LEFT JOIN req_distributions_all rda on (rda.distribution_id = da.req_distribution_id)
LEFT JOIN requisition_lines_all rla on (rda.requisition_line_id = rla.requisition_line_id)
LEFT JOIN requisition_headers_all rha on (rla.requisition_header_id = rha.requisition_header_id)
LET JOIN releases_all ra on (lla.release_id = ra.release_id)
LEFT JOIN head_all contract on (la.contract_num = contract.segment1)
LEFT JOIN head_all blanket_agreement on (la.from_header_id = blanket_agreement.header_id)
LEFT JOIN line_types_b ltb on (lla.line_id = ltb.line_type_id)
WHERE line_locations_all.shipment_type NOT IN ( 'PRICE BREAK', 'PLANNED' )
AND head_all.type_lookup_code IN ( 'BLANKET', 'STANDARD', 'PLANNED' )
)
Select * from dtl
Where da_last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
UNION ALL
SELECT * from dtl
Where da_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND lla_last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
UNION ALL
SELECT * From dtl
Where da_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND lla_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND la_last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
UNION ALL
SELECT * From dtl
Where da_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND lla_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND la_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND ha_last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
UNION ALL
SELECT * From dtl
WHERE da_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND lla_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND la_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND ha_last_update_date <= TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
AND ra_last_update_date > TO_DATE('07/01/2018 00:00:01','MM/DD/YYYY HH24:MI:SS')
注意:在with子句中,所有其他必填列都应使用适当的别名来列出(这就是我放置....的原因)