有没有理由不在 Oracle 中使用视图?

发布于 2024-11-23 15:41:18 字数 238 浏览 3 评论 0原文

我最近注意到我的公司(而且是一家大公司)没有人使用视图。

我想创建一些视图主要是因为它们使我的查询看起来更简单,并且这些视图位于不太频繁更新的表上(每天一次)。

我的替代方案是创建一个记录类型的类型表,并在每次调用 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 技术交流群。

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

发布评论

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

评论(6

執念 2024-11-30 15:41:18

当存在性能影响时,美学在 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:

  • layering views (one view based on another) is a bad practice -- errors won't be encountered until the view is run
  • joining views to other tables and or views is highly suspect -- the optimizer might not see things as well if the underlying query is in place of the view reference. I've had such experiences, because the views joined to were doing more than what the query needed -- sometimes, the queries from all the views used were condensed into a single query that ran much better.

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.

只是一片海 2024-11-30 15:41:18

在这种情况下,听起来创建一些视图确实会有帮助。

您是否询问过周围的人,看看为什么没有人使用视图?这看起来很奇怪,并且肯定表明您没有非常有效地重用 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.

那小子欠揍 2024-11-30 15:41:18

不使用可能有效或无效的视图的一个原因是它们有可能在没有任何视图的情况下创建复杂性

例如我可以写

CREATE VIEW foo as <SOME COMPLEX QUERY>

然后稍后我可以写

CREATE Procedure UseFoo as 
BEGIN
       SELECT 
          somefields
       FROM
          x 
          INNER JOIN foo
         .....

所以现在我正在创建对象需要部署、维护、版本控制等...

或者我可以编写

CREATE Procedure UseFoo as 
BEGIN
       WITH foo as (<SOME COMPLEX QUERY>)
       SELECT 
          somefields
       FROM
          x 
          INNER JOIN foo
         .....

CREATE Procedure UseFoo as 
BEGIN

       SELECT 
          somefields
       FROM
          x 
          INNER JOIN <SOME COMPLEX QUERY> foo
         .....

现在我只需要部署、维护和版本控制单个对象。

如果 仅存在于一个上下文中,则维护两个单独的对象会产生不必要的负担。此外,部署后的任何更改都需要评估依赖于 UseFoo 的内容。当两个对象需要访问对 UseFooFoo 进行评估的任何对象时,

当然,另一方面,如果 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

CREATE VIEW foo as <SOME COMPLEX QUERY>

then later I could write

CREATE Procedure UseFoo as 
BEGIN
       SELECT 
          somefields
       FROM
          x 
          INNER JOIN foo
         .....

So now I'm creating to objects that need to be deployed, maintained, version controlled etc...

Or I could write either

CREATE Procedure UseFoo as 
BEGIN
       WITH foo as (<SOME COMPLEX QUERY>)
       SELECT 
          somefields
       FROM
          x 
          INNER JOIN foo
         .....

or

CREATE Procedure UseFoo as 
BEGIN

       SELECT 
          somefields
       FROM
          x 
          INNER JOIN <SOME COMPLEX QUERY> foo
         .....

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 on UseFoo. When two object you need to visit anything that evaluating on UseFoo and Foo

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.

花落人断肠 2024-11-30 15:41:18

根据我的经验,当您有一个大型/复杂的数据库和一些复杂的查询并且没有视图时,这只是因为用户不知道视图是什么,或者如何使用它们。一旦我解释了使用视图的好处,大多数人就可以毫无问题地使用它们。

根据您的描述,我只会创建一个视图,而不是一个新表。

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.

探春 2024-11-30 15:41:18

视图非常适合隐藏复杂性——如果您的用户可以按原样运行您创建的视图(而不是针对视图编写查询),那么这很好。

但视图也会带来性能问题——如果您的用户知道如何编写 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.

不再让梦枯萎 2024-11-30 15:41:18

这是一篇好文章的链接和片段,该文章描述了视图以及如何调整它们以获得更好的性能。

视图的使用

视图对于提供水平或垂直的数据子集很有用
从表中(可能出于安全原因);用于隐藏
查询的复杂性;确保使用完全相同的 SQL
整个申请过程中;并在 n 层应用程序中检索
相关表中某个项目的补充信息......

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.

Uses of Views

Views are useful for providing a horizontal or vertical subset of data
from a table (possibly for security reasons) ; for hiding the
complexity of a query; for ensuring that exactly the same SQL is used
throughout your application; and in n-tier applications to retrieve
supplementary information about an item from a related table......

http://www.smart-soft.co.uk/Oracle/oracle-tuning-part4-vw-use.htm

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