2017-06-15 121 views
0

我有一個查詢應該返回10000行左右。數據庫本身非常大。我已經運行了一個簡單的查詢,並在不到3秒的時間內返回了一個結果。但是,當一個更復雜的代碼花費太長時間。爲什麼查詢時間太長

在我的代碼中,我做了一個嵌套的select和一個case語句。但是,當我運行我的代碼時,需要一個多小時才能返回結果。我能做些什麼來減少這個執行時間的代碼。

SELECT ticker_symb, day_sum_eff, cusip, 
clos_prc, 
nclos_prc, 
case 
    when clos_prc is null and nclos_prc is not null 
    then (nclos_prc - LAG(nclos_prc ignore nulls) OVER (ORDER BY cusip)) 
    when clos_prc is not null and nclos_prc is null 
     then (LEAD(nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG(nclos_prc ignore nulls) OVER (ORDER BY cusip)) 
     else NULL 
    end DIFF 
FROM (SELECT 
     day_sum_eff, 
     cusip, 
     ticker_symb, 
     clos_prc, 
     nclos_prc, 
     case 
      when clos_prc is null and nclos_prc is not null 
      then (nclos_prc - LAG(nclos_prc ignore nulls) OVER (ORDER BY cusip)) 
      when clos_prc is not null and nclos_prc is null 
      then LEAD(nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG(nclos_prc ignore nulls) OVER (ORDER BY cusip) 
      else NULL 
      end DIFF 
    from MKTDATA.MARKET_DAILY_SUMMARY 
    WHERE day_sum_eff >= '1-JUN-2017' and 
      day_sum_eff <= '10-JUN-2017') 
order by day_sum_eff_,fmr_iss_cusip OFFSET 0 ROWS FETCH NEXT 3 ROW ONLY; 

EXCUTION計劃表

PLAN_TABLE_OUTPUT

計劃哈希值:831959278

---------------------------------------------------------- 
| Id | Operation     | Name     | 
---------------------------------------------------------- 
| 0 | SELECT STATEMENT   |      | 
| 1 | VIEW      |      | 
| 2 | WINDOW SORT PUSHED RANK |      | 
| 3 | WINDOW SORT   |      | 
| 4 |  PARTITION RANGE SINGLE|      | 
| 5 |  TABLE ACCESS FULL | MARKET_DAILY_SUMMARY | 
---------------------------------------------------------- 
+0

你有適當的索引嗎? –

+0

該查詢的執行計劃在哪裏?桌上有哪些索引可用? –

+0

爲什麼你在外部查詢中重複整個CASE事件?爲什麼不選擇'diff'? – APC

回答

0

試試這個

WITH q1 AS (
SELECT 
     day_sum_eff, 
     cusip, 
     ticker_symb, 
     clos_prc, 
     nclos_prc, 
     case 
      when clos_prc is null and nclos_prc is not null 
      then (nclos_prc - LAG(nclos_prc ignore nulls) OVER (ORDER BY 
cusip)) 
      when clos_prc is not null and nclos_prc is null 
      then LEAD(nclos_prc ignore nulls) OVER (ORDER BY cusip)- LAG( 
nclos_prc ignore nulls) OVER (ORDER BY cusip) 
      else NULL 
      end DIFF 
    from MKTDATA.MARKET_DAILY_SUMMARY 
    WHERE day_sum_eff >= '1-JUN-2017' and 
      day_sum_eff <= '10-JUN-2017') 
) 
SELECT ticker_symb, 
     day_sum_eff, 
     cusip, 
     clos_prc, 
     nclos_prc, 
     diff 
    FROM q1 
ORDER BY day_sum_eff_,fmr_iss_cusip OFFSET 0 ROWS FETCH NEXT 3 ROW ONLY 
+0

我跑的代碼,它已經超過2分鐘,並計數... – ana

+0

它可能是代碼以外的東西? – ana

0

試試這個: -

在day_sum_eff列上創建索引,然後再次運行查詢並查看執行時間是否有變化。

這可能是工作。