有没有理由不在 Oracle 中使用视图?
我最近注意到我的公司(而且是一家大公司)没有人使用视图。
我想创建一些视图主要是因为它们使我的查询看起来更简单,并且这些视图位于不太频繁更新的表上(每天一次)。
我的替代方案是创建一个记录类型的类型表,并在每次调用 SP 时填充它。这比使用视图更好吗? (我的猜测是否定的)
PS:数据库是oracle 10g并且 编辑: - 是的,我到处打听,但没有人能给我一个理由。 - 视图和使用它们的查询都依赖于连接。
I have recently noticed that nobody uses views in my company (and it's a big company).
I want to create a few views largely because they make my queries simpler to the eye, and these views are on somewhat big tables that don't get very frequent updates (once a day).
My alternative is to create a type table of type record an populate it each time a SP is called. Is this better than using a view? (my guess is no)
PS: database is oracle 10g and
EDIT:
- yes i have asked around but no one could give me a reason.
- both the views and the queries that will be using them are heavy on joins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当存在性能影响时,美学在 SQL 或一般编码中没有一席之地。
如果优化器确定可以发生谓词推送,则视图将与直接查询视图表示的表一样好。正如 Justin 提到的,因为视图是一个扩展为视图所代表的底层查询的宏,所以很可能会进行软解析(重新使用缓存中的查询),因为缓存检查需要精确匹配查询。
但请注意以下几点:
我建议创建您的视图,并比较 EXPLAIN 计划,以确保您至少获得相同的性能。在评论该方法之前,我需要查看您用于填充类型的代码,但它本质上听起来像派生表...
您可能会从使用物化视图中受益,但众所周知,它们在支持的内容方面受到限制。
Aesthetics doesn't have a place in SQL, or coding in general, when there's performance implications.
If the optimizer determines that predicate pushing can occur, a view will be as good as directly querying the table(s) the view represents. And as Justin mentions, because a view is a macro that expands into the underlying query the view represents -- a soft parse (re-use of the query from cache) is very likely because the cache check needs to match queries exactly.
But be aware of the following:
I recommend creating your views, and comparing the EXPLAIN plans to make sure that you are at least getting identical performance. I'd need to see your code for populating a TYPE before commenting on the approach, but it sounds like a derived table in essence...
It's possible you would benefit from using materialized views, but they are notorious restricted in what they support.
在这种情况下,听起来创建一些视图确实会有帮助。
您是否询问过周围的人,看看为什么没有人使用视图?这看起来很奇怪,并且肯定表明您没有非常有效地重用 SQL。如果没有视图,您可能会倾向于将相同的逻辑放在许多不同的 SQL 语句中,而不是放在单个视图中,这会使维护变得很痛苦。
It certainly sounds like creating some views would be helpful in this case.
Have you asked around to see why no one uses views? That seems quite odd and would certainly tend to indicate that you're not reusing your SQL very efficiently. Without views, you'd tend to put the same logic in many different SQL statements rather than in a single view which would make maintenance a pain.
不使用可能有效或无效的视图的一个原因是它们有可能在没有任何视图的情况下创建复杂性
例如我可以写
然后稍后我可以写
所以现在我正在创建对象需要部署、维护、版本控制等...
或者我可以编写
或
现在我只需要部署、维护和版本控制单个对象。
如果
仅存在于一个上下文中,则维护两个单独的对象会产生不必要的负担。此外,部署后的任何更改都需要评估依赖于UseFoo
的内容。当两个对象需要访问对UseFoo
和Foo
进行评估的任何对象时,当然,另一方面,如果
Foo
表示一些共享逻辑,则评估是无论如何都需要,但您只需查找并更改一个对象。One reason not to use views which may or may not be valid... is that they have the potential to create complexity where there isn't any
For example I could write
then later I could write
So now I'm creating to objects that need to be deployed, maintained, version controlled etc...
Or I could write either
or
And now I only need to deploy, maintain, and version control a single object.
If
<SOME COMPLEX QUERY>
only exists in one context maintaining two separate objects creates an unnecessary burden. Also after deployment any changes to requires evaluating things that rely onUseFoo
. When two object you need to visit anything that evaluating onUseFoo
andFoo
Of course on the other hand if
Foo
represents some shared logic the evaluation is required anyway but you only have to find and change a single object.根据我的经验,当您有一个大型/复杂的数据库和一些复杂的查询并且没有视图时,这只是因为用户不知道视图是什么,或者如何使用它们。一旦我解释了使用视图的好处,大多数人就可以毫无问题地使用它们。
根据您的描述,我只会创建一个视图,而不是一个新表。
It has been my experience that when you have a large/complex database and some complex queries and no views, it is just because the users just don't know what views are, or how to use them. Once I explained the benifits of using a view, most people used them with out any problems.
From your description, I would just make a view, not a new table.
视图非常适合隐藏复杂性——如果您的用户可以按原样运行您创建的视图(而不是针对视图编写查询),那么这很好。
但视图也会带来性能问题——如果您的用户知道如何编写 sql,并且他们了解他们正在使用的表,那么让他们继续这样做可能会更好。
还要考虑到存储过程不像视图那样容易出现(相同的)性能问题。
Views are great for hiding complexity -- if your users can just run the views you create as-is (as opposed to writing a query against the view), this is good.
But views also come with performance issues -- if your users know how to write sql, and they understand the tables they're using, it might be better to let them keep doing that.
Consider also that stored procedures are less prone to (the same) performance issues that views are.
这是一篇好文章的链接和片段,该文章描述了视图以及如何调整它们以获得更好的性能。
http://www.smart-soft.co.uk/Oracle/oracle-tuning-part4-vw-use.htm
here is a link to and a snippet from a nice article that describes views as well as how to tune them for better peformance.
http://www.smart-soft.co.uk/Oracle/oracle-tuning-part4-vw-use.htm