Oracle 中的视图比自然连接更快吗?那么 postgresql 呢?

发布于 2024-10-14 02:33:57 字数 470 浏览 3 评论 0原文

SELECT cec.*
  FROM mam.category cec


SELECT cec.year, ces.*
  FROM mam.subcategory ces
  JOIN mam.category cec ON CEC.CATEGORY_ID = CES.CATEGORY_ID


SELECT cec.year, ceo.*
  FROM mam.options ceo
  JOIN mam.subcategory ces ON CES.SUBCATEGORY_ID = CEO.SUBCATEGORY_ID
  JOIN olr.iep_cost_est_category cec ON CEC.CATEGORY_ID = CES.CATEGORY_ID

据一位朋友说,oracle 中的视图对于缓存来说实际上更快。这是真的吗?那么 postgresql 呢?我尝试过 google 和 stackoverflow (最接近的是 MS SQL)。

SELECT cec.*
  FROM mam.category cec


SELECT cec.year, ces.*
  FROM mam.subcategory ces
  JOIN mam.category cec ON CEC.CATEGORY_ID = CES.CATEGORY_ID


SELECT cec.year, ceo.*
  FROM mam.options ceo
  JOIN mam.subcategory ces ON CES.SUBCATEGORY_ID = CEO.SUBCATEGORY_ID
  JOIN olr.iep_cost_est_category cec ON CEC.CATEGORY_ID = CES.CATEGORY_ID

According to a friend, views in oracle are actually faster for cache purposes. Is this true? What about postgresql? I've tried google and stackoverflow (closest one is MS SQL).

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

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

发布评论

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

评论(2

怀中猫帐中妖 2024-10-21 02:33:57

视图有时可以稍微帮助缓存。
其基础是,

SELECT a.name, b.zipcode 
FROM table_a a JOIN table_b b ON a.id = b.id

但这是一个不同的查询

SELECT a.name, b.zipcode 
FROM table_b b JOIN table_a a ON a.id = b.id

尽管它们在逻辑上是相同的, 。如果两者都发送到 Oracle,它们最终都会进入查询缓存。 [查询缓存是 Oracle 存储查询的位置,因此不必重复语法/权限检查和计算查询执行路径。] 通过使用封装 table_a 和 table_b 之间联接的视图,多个查询结束的可能性较小逻辑上相同的缓存中的内容。

这是“不要重复自己”这一更通用原则的一部分。如果重复代码,则需要重复测试和修补,并且有更多可能出错的代码。任何绩效收益都是奖金。因此,视图有好处,但性能并不重要。

Views can sometimes help with caching slightly.
The basis is that

SELECT a.name, b.zipcode 
FROM table_a a JOIN table_b b ON a.id = b.id

is a different query from

SELECT a.name, b.zipcode 
FROM table_b b JOIN table_a a ON a.id = b.id

even though they are logically identical. If both get sent to Oracle, they both end up in the query cache. [The query cache is where Oracle stores queries so it doesn't have to repeat syntax/permission checks and the calculation a query execution path.] By having a view that encapsulates the join between table_a and table_b, there's less chance of multiple queries ending up in the cache that are logically identical.

This is part of a more generic principle of "Don't repeat yourself". If you repeat code, you need to repeat testing and patching and you have more code that can go wrong. Any performance benefit is a bonus. So there are benefits to views, but performance isn't a significant one.

我做我的改变 2024-10-21 02:33:57

视图

视图,即非物化视图,不会被缓存。它们只是一个准备好的 SQL 语句,用于代替查询中的视图引用运行。将它们视为宏或保存视图中包含的 SELECT 语句的变量。

物化视图(PostgreSQL 不支持)与表类似,因为它们可以建立索引。但众所周知,物化视图在它们可以支持的内容方面受到限制(即:没有非确定性值)。

自然连接

您发布的所有示例都不是自然连接,如下所示:

      SELECT cec.year, ces.*
        FROM mam.subcategory ces
NATURAL JOIN mam.category cec

该语法不受欢迎(尽管是 ANSI),因为它充其量是不明确的,并且在以下情况下会让您面临问题:

  • 添加或重命名的列
  • 不超过两个表 如果列以不寻常的方式跨表连接,则使用此方法可以连接,
  • 您几乎无法控制连接的细节。连接标准是什么并不明显——明确既可读又保证结果一致。

结论

非物化视图在很大程度上与 JOIN 语法无关。数据和索引会对性能产生较大影响。

Views

Views, which means non-materialized views, are not cached. They are simply a prepared SQL statement that is run in place of the view reference in a query. Think of them like macros, or variables holding the SELECT statement contained in the view.

Materialized views (not supported by PostgreSQL) are similar to tables because they can be indexed. But materialized views are notoriously restricted (IE: no non-deterministic values) in what they can support.

Natural JOINs

None of the examples you posted are natural JOINs, which look like this:

      SELECT cec.year, ces.*
        FROM mam.subcategory ces
NATURAL JOIN mam.category cec

The syntax is frowned upon (though being ANSI) because it's ambiguous at best and leaves you open to problems if:

  • columns get added or renamed
  • no more than two tables can be joined using this method
  • gives you little control over the specifics of a join if columns join across the tables in an unusual way. It's not obvious what the join criteria is -- being explicit is both readable and guarantees consistent results.

Conclusion

Non-materialized views are largely irrelevant with regard for JOIN syntax. Data and indexing will have a larger impact on performance.

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