why query is taking too long

陌路散爱 提交于 2020-01-17 06:41:50

问题


I have a query that should return around 10000 rows. The db itself is very large. I have run a simple query and it returned a result in less than 3 seconds. But when a more complex code it takes way too long.

In my code I have done a nested select and a case statement. However, when I run my code it takes over an hour to return a result. What can I do to the code that would decrease this execution time.

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

PLAN_TABLE_OUTPUT

Plan hash value: 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 |
----------------------------------------------------------

回答1:


Try This:-

Create index on day_sum_eff column then run query once again and see will be any changes in execution time or not.

It may be work.




回答2:


Try this

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


来源:https://stackoverflow.com/questions/44573964/why-query-is-taking-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!