Results 1 to 4 of 4

Thread: SQL Server 2005 Views

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    SQL Server 2005 Views

    I have a question on view behavior in SQL Server.

    Lets say I have a proc that is based on some views.

    We start with View1 Which is based on View2 and View 3 and View4. View 2 is also based in part on View2 as is View 4.

    Does View2 get repopulated for each or do all simply access the data in the View2 after is is called in View1?

    Gary
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Server 2005 Views

    A standard View is simply a query that creates a "virtual table". The data it returns is not stored in the database so the View's query is executed everytime it is referenced. It really is no different than executing a statement such as

    Select * From (Select * From Table2 Where ...) As Data Where...

    With the scenario you described though, SQL Server will try and optimize all the queries from all the Views into a single execution plan.

    However, if you create an Index on a View then the View's data is stored in the database. Now whenever the View is referenced only the View's data needs to be queried. But don't go and add an Index to every View because each update/insert into one of the underlying tables in a View's query requires the need to refresh the View's data (sql server does this automatically).
    Last edited by brucevde; Mar 6th, 2009 at 02:20 PM.

  3. #3
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: SQL Server 2005 Views

    Indexed views should be created carefully. And remember when you create an Indexed view, the underlying tables cannot be changed (altered in anyway) unless the dependency of the view is removed.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server 2005 Views

    Yes and the reading I have done says you can't create the indexed view when using Outer Joins..... 3 of the 4 views involved use Left Outer Join Isn't that fun for performance.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width