为什么这两个查询之间的性能增益存在差异?

发布于 2024-09-15 21:55:27 字数 751 浏览 2 评论 0原文

选择 instmax,
r
来自
(选择 instmax,
rownum r
来自
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
WHERE rownum <= 10
)
其中 r >=6;

输出

alt text

<块引用>

选择 instmax,
r
来自
(选择 instmax,
rownum r
来自
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
)
其中 r 介于 6 和 10 之间;

输出

alt text

这两个查询之间真的有明确的性能增益吗?您能澄清一下吗?

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

alt text

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

alt text

Is there really a definite performance gain among both the query?Can you please clarify me on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-09-22 21:55:27

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 use STOPKEY (since it uses unaliased ROWNUM < 10)

You may want to read this article:

如日中天 2024-09-22 21:55:27

这可能是因为 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文