Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Does the optimizer handle joining views to views?


Does the optimizer handle joining views to views?

Author
Message
sqlnyc
sqlnyc
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 865
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
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24195 Visits: 37960
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...

Cool
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)
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
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
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
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
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5697 Visits: 7660
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
sqlnyc
sqlnyc
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 865
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 --
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4253 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search