简体   繁体   English

Sql Server 2008嵌套视图

[英]Sql Server 2008 Nesting Views

Is there a general best practice on whether or not to use nested views? 是否使用嵌套视图是否有一般的最佳实践? Is there a performance hit when using nested views? 使用嵌套视图时是否有性能损失? Is there a best practice that says there really isn't a performance hit until you go 4 or more layers deep? 是否有一种最佳实践表明,在你深入4层或更深层之前,确实没有性能影响?

The reason I'm asking this is because I'm struggling with whether or not to use them. 我问这个的原因是因为我在努力决定是否使用它们。 It is not unusual to get a report request of which the only way I can get access to that information is by joining 20 or more tables together. 获取报告请求并不罕见,我可以访问该信息的唯一方法是将20个或更多表连接在一起。 Fields are not returned from all the tables but are needed to select the correct data. 不从所有表返回字段,但需要选择正确的数据。 In this case I like nesting the views and reusing the lower level views for other reports because if a change to the logic is needed I just update one view and all reports are updated. 在这种情况下,我喜欢嵌套视图并重用其他报表的低级视图,因为如果需要更改逻辑,我只需更新一个视图,所有报表都会更新。 Many of the tables I work with contain millions and millions of records. 我使用的许多表都包含数百万条记录。

However perhaps this is not a good practice. 不过这可能不是一个好习惯。 Do you mind sharing your thoughts on this? 你介意分享你的想法吗?

I would avoid this at all costs. 我会不惜一切代价避免这种情况。 First once you nest views they cannot be indexed. 首先,一旦您嵌套视图,它们就无法编入索引。 Next, since they have to fully materialize the underlying views to get to the next layer. 接下来,因为他们必须完全实现底层视图才能到达下一层。 So you could be materializing multi-millions of records to get an end result of 5 records. 因此,您可以实现数百万条记录,以获得5条记录的最终结果。 We very nearly lost a multimillion dollar client because performance was so abysmal when our devs did this on one database (not a database I had input into the design of). 我们几乎失去了一个价值数百万美元的客户端,因为当我们的开发人员在一个数据库(而不是我输入到设计中的数据库)中执行此操作时,性能非常糟糕。

Finally I have found that these sorts of layers are much, much harder to maintain when you need to make a change. 最后,我发现当你需要做出改变时,这些层很难维护。 It's no fun to track through 12 layers of views to find the one you need to fix. 跟踪12层视图以找到您需要修复的视图并不是一件好事。 We also ran into an issue because devs found it easier just to add another layer than fix the underlying layers and then were trying to access too many tables in one query and way too many of those tables were the same multi-million record table being accessed 7 or 8 times in different layers of the views. 我们还遇到了一个问题,因为开发人员发现添加另一个层比修复底层层更容易,然后尝试在一个查询中访问太多表,并且这些表中的太多表是被访问的相同的数百万个记录表在不同层次的视图中有7或8次。

There is no circumstance where I would allow more than one layer in a view in a database I manage and I'd be angry if you did that. 在我管理的数据库中,我不允许在视图中允许多个图层,如果你这样做,我会生气。

Other options to consider: Indexed Views -- Can be dangerous to use if not used correctly but the performance gains can be amazing. 其他需要考虑的选项: 索引视图 - 如果使用不正确可能会有危险,但性能提升可能会很惊人。

Analytics -- such as grouping sets 分析 - 例如分组集

procedures & temp tables -- Get the data you need via procedure write it out to temp tables select from temp tables. 过程和临时表 - 通过过程获取所需的数据,将其写入临时表,从临时表中选择。

Overall I don't like the performance hit of view on view on view or nested views. 总的来说,我不喜欢在视图或嵌套视图上查看视图的性能。

Generally you can generate one view using the correct joins between tables which contains all the information your after and filter out the data using criteria. 通常,您可以使用表之间的正确连接生成一个视图,其中包含您之后的所有信息并使用条件过滤掉数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM