此查询由 16 个相等的步骤组成。
每一步都在同一个数据集(单行)上进行相同的计算,
但最后一步花费了太多时间。
with t0 as (select 0 as k from dual)
,t1 as (select k from t0 where k >= (select avg(k) from t0))
,t2 as (select k from t1 where k >= (select avg(k) from t1))
,t3 as (select k from t2 where k >= (select avg(k) from t2))
,t4 as (select k from t3 where k >= (select avg(k) from t3))
,t5 as (select k from t4 where k >= (select avg(k) from t4))
,t6 as (select k from t5 where k >= (select avg(k) from t5))
,t7 as (select k from t6 where k >= (select avg(k) from t6))
,t8 as (select k from t7 where k >= (select avg(k) from t7))
,t9 as (select k from t8 where k >= (select avg(k) from t8))
,t10 as (select k from t9 where k >= (select avg(k) from t9))
,t11 as (select k from t10 where k >= (select avg(k) from t10))
,t12 as (select k from t11 where k >= (select avg(k) from t11)) -- 0.5 sec
,t13 as (select k from t12 where k >= (select avg(k) from t12)) -- 1.3 sec
,t14 as (select k from t13 where k >= (select avg(k) from t13)) -- 4.5 sec
,t15 as (select k from t14 where k >= (select avg(k) from t14)) -- 30 sec
,t16 as (select k from t15 where k >= (select avg(k) from t15)) -- 4 min
select k from t16
子查询 t10 立即完成,但整个查询 (t16) 需要 4 分钟才能完成。
Q1。
为什么相同数据的相同子查询的计算时间相差很大?
Q2。
它看起来像一个错误,因为它在 Oracle 9 上运行得非常快,而在 Oracle 11 上运行得非常慢。
事实上,每个带有长而复杂的 with 子句的 select 语句都会以相同的方式运行。
这是一个已知的错误吗?(我无法访问metalink)
推荐什么解决方法?
Q3。
我必须为 Oracle 11 编写代码,并且必须在单个 select 语句中完成所有计算。
我不能将我的长语句分成两个单独的语句来加快速度。
Oracle 中是否存在使整个查询(t16)在合理的时间内(例如,在一秒钟内)完成的提示(或者可能是一些技巧)?我试图找到这样一个,但无济于事。
顺便说一句,执行计划非常好,成本表现为步数的线性函数(不是指数)。