Search code examples
sql-serversubqueryviewquery-optimizationsimplification

Simplify SQL Server Query by Eliminating Views


I am currently working on a very complex view that in turn retrieves data from several other views. I am worried that if someone changes one of my source views, my query will stop working.

Is there a way on how SQL Server can provide me with a query that achieves the same result but uses source tables (instead of views), thus eliminating the need for intermediary views and improving performance by omitting redundant joins?


Solution

  • Look at the view code in design view in SQL server management studio. Copy the SQL code and assign the select output of a view into a temp table in your SQL Stored procedure. Now you have a copy inside SP and you do not need to depend on view.

    Based on my experience I've found that often view has redundant/repeated data columns which need complex calculations and are usually not needed by everything that consumes those views.

    Also by removing nesting, you will get a performance gain.

    Alternatively, if you are not worried about performance, you can duplicate a view and label it differently too.