Does the optimizer handle joining views to views?

  • I have read at least one post by Grant Fritchey stating that the optimizer can have issues when joining views to views. I've no doubt that he is correct, but was just wondering if others can confirm this to be an issue, and if there is a specific set of circumstances that makes it happen.

    I've recently taken hold of an application where view-to-view joins occur everywhere. There are numerous performance problems with this system, and this is just one potential facet. I'm trying to prioritize my approach to dealing with the sluggish-ness of the system.

    Thanks in advance --

    sqlnyc

  • Have no doubt - joining views to views can be very problematic when the view definitions include JOINs. Typically, joining views that SELECT from only one table (as you might have when using views as an abstraction layer to limit access to the underlying tables, for example) won't suffer from this problem, though.

    One way to see how much this issue is affecting your database would be to rewrite queries that join views to return the same results by querying the underlying tables directly, then comparing the execution plans, I/O stats, etc.

    Jason Wolfkill

  • And it gets worse if the views your are joining together are based on views joined together.

    You can imaging how this can expand to views joining views, joining views...

  • And then throw some cursor action on top of that...sweet RBAR! Or you could save yourself all the effort and just put your application on a x286.. lol

    Erin

  • Lynn Pettis (8/9/2012)


    And it gets worse if the views your are joining together are based on views joined together.

    You can imaging how this can expand to views joining views, joining views...

    I started to feel a little ill thinking about that, so I just decided not to go there!

    Jason Wolfkill

  • In theory, non-indexed views are supposed to be broken down into their component pieces and eventually reassembled as a huge nested query.

    In reality, those huge nested queries, even when directly written, rarely behave as planned. Couple this with aggregation subqueries, extraneous data and joins, and any other number of concerns and view nesting can be very problematic. In general, I recommend you create views only for very common requests and try to avoid significant nesting.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Thanks very much to all who have replied. This hornet's nest of a database that I've inherited has every conceivable worst practice. Nested view joining is just one of the many....but it's "character building", I suppose.

    Thanks again --

  • wolfkillj (8/9/2012)


    One way to see how much this issue is affecting your database would be to rewrite queries that join views to return the same results by querying the underlying tables directly, then comparing the execution plans, I/O stats, etc.

    And this is remarkably easy to do!

    Simply extract the VIEW code and put it into a CTE. If more than one VIEW is involved, use more than one CTE. If a VIEW references a VIEW, take that VIEW's code into a CTE also.

    That whole process should take about 1 minute per VIEW. Then time the results of this query against the original. Voila! instant knowledge of whether there's an issue there.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply