gpt4 book ai didi

sql - 查询 View 比直接执行 SQL 慢吗?

转载 作者:行者123 更新时间:2023-12-04 04:13:43 25 4
gpt4 key购买 nike

场景:

我有 3 个表需要连接在一起,一个 where 子句来限制结果集,并且每个表中只有几列被选中。简单。但是,执行此操作的查询并不是很漂亮,并且在数据库和应用程序之间使用 ORM 时,就像尝试将方钉插入圆孔中一样。

我解决这个问题的方法是创建一个包含查询的 View ,现在我的应用程序模型直接映射到数据库中的 View ;不再疯狂映射 ORM 层。

题:
假设这里没有其他因素起作用,如果我直接执行 SQL 语句,针对 View 的查询是否会导致任何额外的性能损失? - 这不是索引 View ,假设相同的 where 子句,保持简单。

我被引导相信一个 View 会受到“正在构建”的额外开销的影响。我的理解是,在所有其他方面都相同的情况下,两者应该具有相同的性能。

请说清楚。谢谢!

最佳答案

来自 MSDN:
View resolution

When an SQL statement references a nonindexed view, the parser and query optimizer analyze the source of both the SQL statement and the view and then resolve them into a single execution plan. There is not one plan for the SQL statement and a separate plan for the view.



不应该有任何不同的表现。 View 可帮助您进行组织,而不是任何性能增强。除非您使用索引 View 。

Only the definition of a nonindexed view is stored, not the rows of the view. The query optimizer incorporates the logic from the view definition into the execution plan it builds for the SQL statement that references the nonindexed view.

关于sql - 查询 View 比直接执行 SQL 慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4630633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com