为什么嵌套视图的解释计划与单个合并视图不同?

发布于 2024-10-05 17:36:11 字数 949 浏览 4 评论 0原文

我有一个视图 V2,它从另一个视图 V1 中进行选择,并添加了几个谓词过滤器。

V2 IS SELECT * FROM V1 
WHERE ACTIVE='Y' 
AND TYPE = '1';

当我在 V2 和表 FOO 上的列 USER_ID(在 FOO 中索引)上进行联接时,我发现它首先计算 V2 的整个结果,然后再与索引列上的 FOO 连接(也在 V1 的源表中建立索引)。

SELECT * FROM FOO 
INNER JOIN V2
  ON FOO.USER_ID = V2.USER_ID
WHERE FOO.SCHOOL = '3'

但是,当我在 V1FOO 之间进行联接时,它首先找到 FOO 中的记录,然后使用索引快速检索 FOO 中的行code>V1 使用谓词推送 &索引。

我已将差异缩小到以下事实:V2 是通过谓词构建在 V1 之上的。我将 V2 更改为 V1 的精确副本,但直接将两个附加谓词过滤器放入其中,发现与 FOO< 连接时它的行为方式相同/code> V1 做到了,但现在有了适当的限制。

嵌套视图是否无法将谓词一直推到表中?另外,谓词推送的一致性如何?我有几种观点,如果不推送谓词,就会破坏性能。他们现在工作得很好,但有什么保证他们会继续这样做呢?

我想嵌套/继承视图,而不是重复具有细微差别的完全相同的查询,以保持事物干燥。

I have a view V2 that selects from another view V1 and adds a couple predicate filters.

V2 IS SELECT * FROM V1 
WHERE ACTIVE='Y' 
AND TYPE = '1';

When I do a join between V2 and a table FOO on the column USER_ID (indexed in FOO) I find that it first calculates the entire results for V2 before joining with FOO on the indexed column (indexed also in the source table of V1).

SELECT * FROM FOO 
INNER JOIN V2
  ON FOO.USER_ID = V2.USER_ID
WHERE FOO.SCHOOL = '3'

But when I do a join between V1 and FOO it finds the record in FOO first and then uses the index to quickly retrieve the row from V1 using predicate pushing & indexes.

I have narrowed down the difference to the fact that V2 is built on top of V1 with predicates. I changed V2 to be an exact copy of V1 but put the two additional predicate filters in it directly and found that it behaved the same way when joined with FOO that V1 did but with the proper restrictions in place now.

Are nested views unable to push predicates all the way down to the tables? Also, how consistent is predicate pushing? I have several views where if predicates are not pushed it would ruin performance. They work fine now but what guarantee is there they will continue to do so?

I'd like to nest/inherit views rather than duplicating the exact same queries with minor differences to keep things DRY.

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

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

发布评论

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

评论(1

佞臣 2024-10-12 17:36:28

您使用什么版本的 Oracle?
参数OPTIMIZER_SECURE_VIEW_MERGING设置为什么?您可以尝试将其设置为 FALSE 或授予用户 MERGE ANY VIEW 权限。

如果这没有帮助,您可以发布两个查询计划吗?

What version of Oracle are you using?
What is the parameter OPTIMIZER_SECURE_VIEW_MERGING set to? You may try setting that to FALSE or granting the user the MERGE ANY VIEW privilege.

If that doesn't help, can you post the two query plans?

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