Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have read somewhere long time ago. The book states that we should not allow to having a nested view in SQL Server. I am not sure the reason why we can't do that or I might remember incorrect statement.

Students

SELECT studentID, first_name, last_name, SchoolID, ... FROM students

CREATE VIEW vw_eligible_student
AS 
SELECT * FROM students
WHERE enroll_this_year = 1

Teachers

SELECT TeacherID, first_name, last_name, SchoolID, ... FROM teachers

CREATE VIEW vw_eligible_teacher
AS 
SELECT * FROM teachers
WHERE HasCert = 1 AND enroll_this_year = 1

Schools

CREATE VIEW vw_eligible_school
AS 
SELECT TOP 100 PERCENT SchoolID, school_name 

FROM schools sh 
JOIN
     vw_eligible_student s 
     ON s.SchoolID = sh.SchoolID
JOIN 
     vw_eligible_teacher t
     ON s.SchoolID = t.SchoolID

At my workplace, I have investigated one of our in-house database application. I checked through the objects found out that there are two or three layers of the view stack each other. So that was remind me about what I read in the past. Can any one help explaining it?

If it is not OK to do so, I want to know that it is limited to just SQL Server or it is for database design in general.

Additional Info: I updated an example from my company. I change a bit to be more general without too many technical (too many columns in this example). Mostly the nested view we used is based on abstract or aggregated view. For example, we have a large student table with hundred of columns. Say, Eligible Student View is based on students who enrolls this year. And student eligible view could be use other places such as in stored-procedure.

share|improve this question
I would submit that the same pros and cons would roughly equate regardless of the specific platform. – Aaron Bertrand Sep 7 '11 at 18:47
Related StackOverflow post. – Nick Chammas Sep 7 '11 at 18:49

2 Answers

up vote 20 down vote accepted

Regardless of platform, the following remarks apply.

(-) Nested views:

  • are harder to understand and debug

    e.g. What table column does this view column refer to? Lemme dig through 4 levels of view definitions...

  • make it harder for the query optimizer to come up with the most efficient query plan

    See this, this, this, and this for anecdotal evidence. Compare to this, which shows that the optimizer is often smart enough to correctly unpack nested views and select an optimal plan, but not without a compilation cost.

    You can measure the performance cost by comparing the view query to an equivalent one written against the base tables.

(+) On the other hand, nested views let you:

  • centralize and reuse aggregations or business rules
  • abstract away your underlying structure (say, from other database developers)

I've found that they are rarely necessary.


In your example you are using nested views to centralize and reuse certain business definitions (e.g. "What is an eligible student?"). This is a valid use for nested views. If you are maintaining or tuning this database, weigh the cost of keeping them against that of removing them.

  • Keep: By keeping the nested views you incur the advantages and disadvantages enumerated above.

  • Remove: To remove the nested views:

    1. You need to replace all occurrences of the views with their base queries.

    2. You must remember to update all relevant queries if your definition of eligible student/teacher/school changes, as opposed to just updating the relevant view definition.

share|improve this answer
1  
+1, except I would replace "harder" for the query optimizer, with "almost impossible". :) – Jason Sep 7 '11 at 18:46
1  
@Jason - I agree, and I wish I could link to some concrete examples. Do you know of any references that explain or demonstrate why this is so? – Nick Chammas Sep 7 '11 at 19:09
1  
All I can find is anecdotal evidence that when nested views get used, they suffer performance problems compared to the "flattened" SQL. sqlservercentral.com/blogs/2cents/archive/2010/04/05/… The problem appears to come down to the fact that the DB (SQL Server in this case) won't apply certain filters before it joins tables, and will therefore make the query take longer than it should. – Jason Sep 7 '11 at 19:56
1  
I disagree on the query optimizer issue, as the resulting query after resolving all views will be the same no matter how many view transformations it has gone through (except some extra columns in intermediate result sets, which the optimizer can eliminate just fine). This leaves debugging; IMO it makes debugging easier to have nested views as I can look at intermediate results to see where it went wrong. – Simon Richter Sep 8 '11 at 0:52
@Simon - These are great objections. With regards to the query optimization issue, for simple nested views the optimizer can indeed pick the appropriate plan. From past experience as well as anecdotal evidence, I gather that it fails to do this for more complex views. See Jason's comment as well as this and this. – Nick Chammas Sep 8 '11 at 1:35
show 2 more comments

Sometimes nested views are used to prevent repeating aggregates. Let's say you have a view that counts messages and groups them by userid, you might have a view over that that counts the number of users that have > 100 messages, that kind of thing. This is most effective when the base view is an indexed view - you don't necessarily want to create yet another indexed view to represent the data with a slightly different grouping, because now you're paying for the index maintenance twice where performance is probably adequate against the original view.

If these are all just nested views where you're doing select * but changing the ordering or top, it seems this would be better encapsulated as a stored procedure with parameters than a bunch of nested views. IMHO.

share|improve this answer
"This is most effective when the base view is an indexed view." Important point. – Nick Chammas Sep 8 '11 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.