简体   繁体   English

Oracle 11与视图连接成本很高

[英]Oracle 11 joining with view has high cost

I'm having some difficulty with joining a view to another table. 将视图连接到另一个表时遇到一些困难。 This is on an Oracle RAC system running 11.2 这是在运行11.2的Oracle RAC系统上

I'll try and give as much detail as possible without going into specific table structures as my company would not like that. 我将尝试提供尽可能多的细节,而不必使用我的公司不希望使用的特定表结构。

You all know how this works. 你们都知道这是如何工作的。 "Hey, can you write some really ugly software to implement our crazy ideas?" “嘿,您能编写一些非常丑陋的软件来实施我们的疯狂想法吗?”

The idea of what they wanted me to do was to make a view where the end user wouldn't know if they were going after the new table or the old table so one of the tables is a parameter table that will return "ON" or "OFF" and is used in the case statements. 他们要我做什么的想法是,查看最终用户不知道他们是要追随新表还是旧表的视图,因此其中一个表是一个参数表,它将返回“ ON”或“ OFF”,在case语句中使用。

There are some not too difficult but nested case statements in the select clause select子句中有一些不太难但嵌套的case语句

I have a view: 我有一个观点:

create view my_view as 
select t1.a as a, t1.b as b, t1.c as c,
sum(case when t2.a = 'xx' then case when t3.a then ... ,
case when t2.a = 'xx' then case when t3.a then ... ,

from table1 t1 
join table t2 on (t1.a = t2.a etc...)
full outer join t3 on (t1.a = t3.a etc...)
full outer join t4 on (t1.a = t4.a etc...)
group by t1.a, t1.b, t2.c, and all the ugly case statements...

Now, when I run the query 现在,当我运行查询时

select * from my_view where a='xxx' and b='yyy' and c='zzz'

the query runs great and the cost is 10. 查询运行良好,成本为10。

However, when I join this view with another table everything falls apart. 但是,当我将此视图与另一个表合并时,一切都崩溃了。

select * from my_table mt join my_view mv on (mt.a = mv.a and mt.b=mv.b and mt.c=mv.c) where ..." 

everything falls apart with a cost though the roof. 一切都通过屋顶瓦解。

What I think is happening is the predicates are not getting pushed to the view. 我认为正在发生的事情是谓词没有被推向视野。 As such, the view is now doing full tables scans and joining everything to everything and then finally removing all the rows. 这样,视图现在将进行全表扫描,并将所有内容连接到所有内容,然后最终删除所有行。 Every hint, tweak, or anything I've done doesn't appear to help. 每个提示,调整或我所做的任何事情似乎都无济于事。

When looking at the plan it looks like it has the predicates. 在查看计划时,看起来好像有谓词。

在此处输入图片说明

But this happens after everything is joined. 但这一切都在加入之后发生。

在此处输入图片说明

Sorry if this is cryptic but any help would be greatly appreciated. 抱歉,如果这是含糊的,则将不胜感激。

Since you have the view with a "GROUP BY", predicates could not be pushed to the inner query 由于您的视图带有“ GROUP BY”,因此无法将谓词推入内部查询

Also, you have the group by functions in a case statement, which could also make it worse for the optimizer 另外,您在case语句中具有group by函数,这也可能使优化程序更糟

Oracle introduces enhancements to Optimizer every version/release/patch. Oracle为Optimizer的每个版本/发行版/补丁引入了增强功能。 It is hard to say what is supported in the version you're running. 很难说您正在运行的版本支持什么。 However, you can try: 但是,您可以尝试:

  1. See if removing the case from the GROUP BY function will make any difference 看看是否从GROUP BY函数中删除案例会有所不同
  2. Otherwise, you have to take the GROUP BY and GROUP BY functions from the view to the outer most query 否则,您必须将GROUP BY和GROUP BY函数从视图中带到最外面的查询

After many keyboard indentations on my forehead I may have tricked Oracle into pushing the predicates. 在额头上出现许多键盘缩进之后,我可能欺骗了Oracle推谓词。 I don't know exactly why this works but simplifying things may have helped. 我不知道为什么会这样,但是简化事情可能有所帮助。

I changed all my ON clauses to USING clauses and in this way the column names now match the columns from which I'm joining to. 我将所有ON子句更改为USING子句,这样,列名现在与我要加入的列匹配。 On some other predicates that were constants I added in a where clause to the view. 在某些其他谓词常量上,我在视图的where子句中添加了该谓词。

The end result is I can now join this view with another table and the cost is reasonable and the plan shows that the predicates are being pushed. 最终结果是,我现在可以将该视图与另一个表连接起来,并且开销是合理的,并且该计划显示谓词已被推送。 在此处输入图片说明

Thank you to everybody who looked at this problem. 感谢所有关注此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM