Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Views sourcing other views

Author  Topic 

srouse
Starting Member

7 Posts

Posted - 2010-02-17 : 16:11:51
Is it bad practice to have a view select from another view?

I am migrating to SQL Server and came across a view that selects from another view. Doesn't seem like a good idea to me, but I'd like some other opinions. I'd rather not fix it now.

Thanks
Scott

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-02-17 : 18:07:21
It's not necessarily a good idea or a bad idea. It just depends on the design of the tables, views, and what indexes exist on those.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 01:05:05
is it an indexed view?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-02-18 : 11:04:09
This is a very interesting one. Application Developers probably come up with this because it is the same mentality to programming: if a function can use another function that uses another function, why can't a view use another view that uses another view?

First we have have to understand views (and I am assuming we are talking about 'regular' views rather than indexed views). SQL server creates one execution plan per QUERY and a View on it's own is not a query until you actually use it in a select statement. When you execute a query that contains views, SQL server will replace the view with the query behind it to come up with one query and then make an execution plan based on that query (unless an execution plan already exists for that query). So if you compare a query without views and the identical query with views, you will get the exact same results, exact same execution plan and exact same execution time.

The problems arise when someone new comes along and then uses that view comprised of several views, each one of which is comprised of several more views. The person person thinks there are writing a very simple query but in actual fact, it is a very long, complicated query that runs as slow as a turtle.
Go to Top of Page
   

- Advertisement -