Nested View- Performance Issue

manish verma 481 Reputation points
2021-08-11T14:45:43.287+00:00

Hi All,

i got a issue to solve issue of performance of a Main view calling in SP, main view calling nested view i.e

Main View- Calling View D
View D- Calling View C
View C- Calling View B
View B calling - View A
View A - Calling Base Table

main view get aggregated measures , now performance of Main View is too slow .

please suggest how to remove these view and get data soon as possible.

what is best practices in this case

Thanks

Azure SQL Database
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
{count} votes

Accepted answer
  1. manish verma 481 Reputation points
    2021-09-14T10:33:55.56+00:00

    replace all View to SP will faster execution.

    Thanks all for suggestion

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 113.3K Reputation points MVP
    2021-08-11T21:29:07.923+00:00

    Since we don't see the code, we cannot say that much.

    A view itself is just a macro and does not call anything. SQL Server expands the view text, and works with the expanded query.

    A common problem with nested views is that the same table may be included many times, when a single time had been enough.

    I would suggest that you first rewrite the query with a number of CTEs:

    ; WITH A AS (
        SELECT .... FROM BaseTable
    ), B AS (
       SELECT ... FROM A
    ), etc
    

    Once you have verified that this query has the same result as the original view, you can start to analyse it to see if there are things you can simplify.

    The query with the CTEs will not run any faster than the view query. A CTE is basically just a view that is local to the query, and again, it's a macro that SQL Server expands into the query. The whole with the CTE is that it makes it easier to trim down the query in complexity.

    1 person found this answer helpful.
    0 comments No comments

  2. Alberto Morillo 34,146 Reputation points MVP
    2021-08-11T17:03:10.133+00:00

    Nested views are one of the most difficult programming objects to troubleshoot performance on SQL Server because they make query plans more complex and you can read in this articile about more reasons.

    My suggestion is the following: start moving the query on view A as subquery into the View definition of view B. Test it. Make sure the query on view B has all the indexes needed. Once you merge view A & view B, merge view B with view C. In the end, you can keep view D only, once you merge C & D. But for me, consider to convert that view D (with all the others views merged into it) to just one stored procedure.

    Procedures are better in many ways on most scenarios. You can use parameters to filter data with them. Each view can have only one query.

    0 comments No comments

  3. EchoLiu-MSFT 14,591 Reputation points
    2021-08-12T05:45:12.89+00:00

    Hi @manish verma ,

    You could solve the problem simply by "decomposing" the nested view and including its full SQL definition inside the calling query. Just doing this alone would take the query back to its quick performance.

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  4. manish verma 481 Reputation points
    2021-08-12T05:48:40.083+00:00

    Thanks a lot for your all answer, l will test approach then finally know which one best, looks like we have multiple option,

    thanks


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.