Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Does the optimizer handle joining views to views? Expand / Collapse
Author
Message
Posted Sunday, August 5, 2012 2:24 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 6:30 AM
Points: 57, Visits: 615
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

Post #1340291
Posted Thursday, August 9, 2012 10:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:21 PM
Points: 1,210, Visits: 2,512
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1342844
Posted Thursday, August 9, 2012 10:45 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:59 PM
Points: 23,002, Visits: 31,490
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...



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1342849
Posted Thursday, August 9, 2012 10:58 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, July 18, 2014 3:36 PM
Points: 541, Visits: 1,044
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
Post #1342856
Posted Thursday, August 9, 2012 11:32 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:21 PM
Points: 1,210, Visits: 2,512
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1342884
Posted Thursday, August 9, 2012 2:10 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:32 PM
Points: 6,139, Visits: 7,190
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1343000
Posted Thursday, August 9, 2012 9:17 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 6:30 AM
Points: 57, Visits: 615
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 --
Post #1343115
Posted Thursday, August 9, 2012 11:10 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,609, Visits: 5,221
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1343137
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse