【发布时间】:2017-06-15 17:56:23
【问题描述】:
我有一个应该返回大约 10000 行的查询。数据库本身非常大。我运行了一个简单的查询,它在不到 3 秒的时间内返回了一个结果。但是当一个更复杂的代码需要太长的时间。
在我的代码中,我做了一个嵌套选择和一个 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;
执行计划表
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 |
----------------------------------------------------------
【问题讨论】:
-
你有合适的索引吗?
-
该查询的执行计划在哪里?表上有哪些可用索引?
-
你为什么在外部查询中重复整个 CASE 事情?为什么不直接选择
diff? -
@APC 为了让我能够访问 DIFF 别名列,我不必将它嵌套在另一个选择中吗?
-
但是你已经有一个外部查询
标签: sql oracle performance long-running-processes