SQL Server 查看性能

发布于 2024-10-10 10:12:10 字数 430 浏览 1 评论 0原文

如果我在 SQL Server 中定义了一个视图,如下所示:

CREATE View V1 
AS
SELECT *    
FROM t1
INNER JOIN t2 ON t1.f1 = t2.f2    
ORDER BY t1.f1

我是否应该期望视图之间存在性能差异

SELECT * FROM V1 WHERE V1.f1 = 100

并避免像这样的视图

SELECT *    
FROM t1
INNER JOIN t2 ON t1.f1 = t2.f2    
WHERE t1.f1 = 100
ORDER BY t1.f1

除了需要集中复杂的查询之外,我们没有任何理由使用视图。

谢谢

If I have defined a view in SQL Server like this:

CREATE View V1 
AS
SELECT *    
FROM t1
INNER JOIN t2 ON t1.f1 = t2.f2    
ORDER BY t1.f1

Should I expect performance differences between

SELECT * FROM V1 WHERE V1.f1 = 100

and just avoiding view, like this

SELECT *    
FROM t1
INNER JOIN t2 ON t1.f1 = t2.f2    
WHERE t1.f1 = 100
ORDER BY t1.f1

?

We don't have any reason to use views except the need to centralize complex queries.

Thanks

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

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

发布评论

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

评论(2

孤独陪着我 2024-10-17 10:12:10

不应该有性能损失。

视图的作用就是简化复杂的查询。

如果您关心性能 - 请阅读 SQL Server 中的索引视图

索引视图提供了使用标准索引无法实现的额外性能优势。索引视图可以通过以下方式提高查询性能:

  • 可以预先计算聚合并将其存储在索引中,以最大限度地减少查询执行期间昂贵的计算。
  • 可以预先连接表并存储结果数据集。
  • 可以存储连接或聚合的组合。

There should be no performance penalty.

Simplifying complex queries is what views are for.

If performance is something you are concerned about - read about indexed views in SQL Server:

indexed views provide additional performance benefits that cannot be achieved using standard indexes. Indexed views can increase query performance in the following ways:

  • Aggregations can be precomputed and stored in the index to minimize expensive computations during query execution.
  • Tables can be prejoined and the resulting data set stored.
  • Combinations of joins or aggregations can be stored.
孤独患者 2024-10-17 10:12:10

通常,您不应期望性能差异,但应检查查询的执行计划。

如果您将视图连接到视图上,那么执行计划可能不是最优的,并且包含对本可以合并的同一表的重复访问。此外,视图和谓词推送< /a>.

Generally you shouldn't expect performance differences but check the execution plans for your queries.

If you are joining Views onto Views then the execution plans can be sub optimal and contain repeated accesses to the same table that could have been consolidated. Also there can be issues with views and predicate pushing.

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