为什么这两个查询之间的性能增益存在差异?
选择 instmax,
r
来自
(选择 instmax,
rownum r
来自
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
WHERE rownum <= 10
)
其中 r >=6;
输出
<块引用>选择 instmax,
r
来自
(选择 instmax,
rownum r
来自
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
)
其中 r 介于 6 和 10 之间;
输出
这两个查询之间真的有明确的性能增益吗?您能澄清一下吗?
SELECT instmax,
r
FROM
(SELECT instmax,
rownum r
FROM
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
WHERE rownum <= 10
)
WHERE r >=6;
Output
SELECT instmax,
r
FROM
(SELECT instmax,
rownum r
FROM
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
)
WHERE r between 6 and 10;
Output
Is there really a definite performance gain among both the query?Can you please clarify me on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Oracle 无法将涉及别名
ROWNUM
的条件推送到内联视图中。这意味着第二个查询将使用全表(或索引)扫描并在
rn
上进行过滤,而第一个查询将使用STOPKEY
(因为它使用非别名ROWNUM < 10
)您可能想阅读这篇文章:
Oracle cannot push conditions that involve aliased
ROWNUM
into the inline views.This means that the second query will use a full table (or index) scan with filtering on
rn
, while the first one will useSTOPKEY
(since it uses unaliasedROWNUM < 10
)You may want to read this article:
这可能是因为 STOPKEY 优化不再起作用。
“rownum < xx”非常特殊(称为 Top-N 查询),具有特殊的优化。之间的子句不再触发它。
您应该比较执行计划并查找“STOPKEY”,如果存在,则它是优化的 Top-N 查询。
另请参阅:
http://blog.fatalmind.com/ 2010/07/30/分析前n个查询/
That's probably because the STOPKEY optimization doesn't work anymore.
The "rownum < xx" is very special (called Top-N query) that has special optimizations. the between clause doesn't trigger that anymore.
You should compare the exeuction plans and look for "STOPKEY", if that is in, it is the optimized Top-N query.
see also:
http://blog.fatalmind.com/2010/07/30/analytic-top-n-queries/