Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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. 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?

like image 497
user742085 Avatar asked May 06 '11 16:05

user742085


People also ask

What is nested views in SQL?

There are mainly two types of nested queries: Independent Nested Queries: In independent nested queries, query execution starts from innermost query to outermost queries. The execution of inner query is independent of outer query, but the result of inner query is used in execution of outer query.

What is nested view?

If a view is based on another view, the number of predicates that must be evaluated is based on the WITH CHECK OPTION specification. If a view is defined without WITH CHECK OPTION, the definition of the view is not used in the data validity checking of any insert or update operations.

Can we do indexing on views?

Creating a unique clustered index on a view improves query performance because the view is stored in the database in the same way a table with a clustered index is stored. The query optimizer may use indexed views to speed up the query execution.

Can you nest tables in SQL?

In order to create a nested table, the two source tables must contain a defined relationship so that the items in one table can be related to the other table. In SQL Server Data Tools, you can define this relationship in the data source view.


2 Answers

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. 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. 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.

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.

like image 67
HLGEM Avatar answered Nov 10 '22 11:11

HLGEM


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.

like image 23
Luke Davis Avatar answered Nov 10 '22 12:11

Luke Davis