2011-10-06 188 views
1

我有一個查詢需要花費很長時間才能在Oracle中完成。它在我殺死它之前跑了幾個小時。有什麼方法可以加快速度嗎?我的查詢需要很長時間才能完成

這裏是我的查詢:

select distinct(random_selection.randnum), 
    random_selection.dropper_id, 
    random_selection.ozip3 
from random_selection 
where random_selection.dropper_id is not null 
and random_selection.quarter = 121 
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in 
    (select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq 
    from forecast_entry, production_weeks 
    where forecast_entry.week = production_weeks.production_week 
    and production_weeks.project_cd = 'EXFC' 
    and production_weeks.yyq >= 121) 

union 

select distinct(random_selection.randnum), 
    dropper_city_brk_2.dropper_id, 
    random_selection.ozip3 
from random_selection, dropper_city_brk_2, dropper 
where random_selection.ozip3 = dropper_city_brk_2.zip3 
and dropper.dropper_id = dropper_city_brk_2.dropper_id 
and dropper.active = 1 
and dropper_city_brk_2.dropper_id <> 10002 
and random_selection.quarter = 121 
and random_selection.dropper_id is null 
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in 
    (select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq 
    from forecast_entry, production_weeks 
    where forecast_entry.week = production_weeks.production_week 
    and production_weeks.project_cd = 'EXFC' 
    and production_weeks.yyq >= 121) 

查詢解釋說:

主要目的是讓所有的randnum,dropper_id,並ozip3從random_selection表不在forecast_entry表,並在yyq 121中有一個項目代碼EXFC。通過關聯周和生產周,可以從production_weeks表中檢索yyq。一些dropper_id爲null,因此我們需要通過關聯ozip3和zip3從dropper_city_brk_2表中提取數據。我們不希望dropper_id處於非活動狀態,因此它們必須有活動的等於1,這是通過關聯dropper表。

希望這有助於

+2

您應該在問題中包含您的表格模式以及您設置的任何索引。 – jadarnel27

+0

這是我在生活中見過的最大的SQL查詢:)讀取它必須要稍作休息 –

+4

我已經看到更大的...... –

回答

1

我同意,你應該提供解釋的要點平原至少輸出提意見,否則是很難幫助。

您正在運行forecast_entry,production_weeks子查詢兩次。通過使用CREATE TEMPORARY TABLE來計算該查詢一次,您可能會獲得更好的性能。

您正在使用「where X in(subquery)」,這通常效率低於進行連接。而不是在這裏使用IN子句,你可以在我剛纔建議的臨時表上進行INNER JOIN。

如果您可以容忍重複或者如果您知道沒有重複項,您可以切換到UNION ALL。 UNION ALL比UNION需要更少的數據庫工作。

最後,你可以把它分成三部分,分別進行測試。這些部分是子查詢和您正在聯合的兩個查詢。這可能會幫助您縮小哪些部件很慢。

相關問題