2017-05-10 26 views
0

下面的sql查詢花費了太多的時間來執行。這可能是由於from子句中重複使用同一個表。我無法找到如何解決這個查詢,以便提高性能。 任何人都可以幫我解決這個問題嗎?如何提高Oracle中的查詢性能

在此先感謝!

select -- 
    from t_carrier_location act_end, 
     t_location   end_loc, 
     t_carrier_location act_start, 
     t_location   start_loc, 
     t_vm_voyage_activity va, 
     t_vm_voyage   v, 
     t_location_position lp_start, 
     t_location_position lp_end 
where act_start.carrier_location_id = va.carrier_location_id 
    and act_start.carrier_id = v.carrier_id 
    and act_end.carrier_location_id = 
     decode((select cl.carrier_location_id 
       from t_carrier_location cl 
       where cl.carrier_id = act_start.carrier_id 
       and cl.carrier_location_no = 
        act_start.carrier_location_no + 1), 
       null, 
       (select cl2.carrier_location_id 
       from t_carrier_location cl2, t_vm_voyage v2 
       where v2.hire_period_id = v.hire_period_id 
        and v2.voyage_id = 
         (select min(v3.voyage_id) 
         from t_vm_voyage v3 
         where v3.voyage_id > v.voyage_id 
          and v3.hire_period_id = v.hire_period_id) 
        and v2.carrier_id = cl2.carrier_id 
        and cl2.carrier_location_no = 1), 
       (select cl.carrier_location_id 
       from t_carrier_location cl 
       where cl.carrier_id = act_start.carrier_id 
        and cl.carrier_location_no = 
         act_start.carrier_location_no + 1)) 
    and lp_start.location_id = act_start.location_id 
    and lp_start.from_date <= 
     nvl(act_start.actual_dep_time, act_start.actual_arr_time) 
    and (lp_start.to_date is null or 
     lp_start.to_date > 
     nvl(act_start.actual_dep_time, act_start.actual_arr_time)) 
    and lp_end.location_position_id = act_end.location_id 
    and lp_end.from_date <= 
     nvl(act_end.actual_dep_time, act_end.actual_arr_time) 
    and (lp_end.to_date is null or 
     lp_end.to_date > 
     nvl(act_end.actual_dep_time, act_end.actual_arr_time)) 
    and act_end.location_id = end_loc.location_id 
    and act_start.location_id = start_loc.location_id; 
+0

請給ER模型和一些示例數據。 –

+0

將相關的子查詢更改爲JOIN – GurV

+0

您是否嘗試過基於數據和要求對錶進行索引。必須妥善決定。 – Raghvendra

回答

1

沒有挺直着一個回答你的問題,你所提到的查詢。 爲了獲得更好的查詢響應時間,在編寫查詢時需要記住幾件事。我將在這裏提到幾個對您的查詢很重要的內容

  1. 使用連接而不是子查詢。
  2. 使用EXPLAIN確定查詢功能正常。
  3. 使用具有where子句索引的列在這些列上創建索引。這裏使用常識,這是列索引例如:外鍵列,刪除,orderCreatedAt,等的startDate
  4. 請選擇列的順序,因爲它們出現在餐桌上,而不是任意選擇列。

以上四點是夠你所提供的查詢。

深挖有關SQL優化和調整是指這個https://docs.oracle.com/database/121/TGSQL/tgsql_intro.htm#TGSQL130

+0

感謝您的寶貴迴應! – Sach