SQL Server 中的视图与临时表或临时变量
我想了解在 SQL Server 存储过程中使用视图而不是临时表的性能优点/缺点。
我知道两者之间的区别(例如临时表可以提供过时的数据等),但我无法在任何地方从性能角度找到任何信息。
I want to understand performance merits/demerits in using views instead of temporary tables in stored procedures in SQL Server.
I know the difference between the two (like temporary table can give stale data, etc.) but I could [not] find any info from performance perspective anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您拥有
企业版
并且您的视图可以遵循索引视图
的规则,否则视图
不会提高性能。调用其他视图的视图可能会导致严重的性能问题。临时表确实有过时的数据,但对于一个过程中具有多个操作的事务来说,这通常是一件好事,因为所有操作都针对同一组数据进行操作。这会减少数据完整性问题。在较小的数据集中,表变量
通常比临时表
更快,但临时表
对于较大的数据集往往表现更好,特别是因为它们可以已编入索引。与所有性能调整一样,根据您的特定硬件和数据库设计,存在差异,因此如果您担心性能(您应该如此),那么您需要测试各种选项以查看 hwat 最适合您的特定实例。Views
will not give improved performance unless you haveEnterprise edition
and your view can follow the rules for anindexed view
. Views which call other views can cause severe performance issues.Temp tables
do have stale data, but for the purposes of a transaction with multiple actions in a proc that is a generally good thing as all actions are operating against the same set of data. That leads to fewer data integrity problems.Table varaibles
are faster thantemp tables
in smaller sets of data generally buttemp tables
tend to perform better for the larger sets especially since they can be indexed. As with all performance tuning, there are differences depending on your particular hardware and database design, so if you are concerned about performance (as you should be) then you need to test the various options to see hwat works best for your particular instance.