问题
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