SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Joining on views in queries instead of tables


Joining on views in queries instead of tables

Author
Message
John Deupree
John Deupree
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1003 Visits: 374
I’m working on a legacy DB and I notice that several of the procedures do joins on views. Some of these views were created on very wide tables to reduce the number of columns returned and reference just the one table. Other views do joins against multiple tables. The query plans of the procedures indicate that, most of the time but not always, the views are using the indexes of the underlying tables. Can someone comment on the performance implications of using views as opposed to the tables themselves.

TIA

John Deupree
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15156 Visits: 18599
Hard to say without further knowledge, normally I would consider this a good and normal practice.
Cool
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26151 Visits: 17539
John Deupree (5/2/2014)
I’m working on a legacy DB and I notice that several of the procedures do joins on views. Some of these views were created on very wide tables to reduce the number of columns returned and reference just the one table. Other views do joins against multiple tables. The query plans of the procedures indicate that, most of the time but not always, the views are using the indexes of the underlying tables. Can someone comment on the performance implications of using views as opposed to the tables themselves.

TIA


A view will use the same indexes. There may be something in the view or the queries joining to them that prevents some indexes from being used. From just a performance perspective there is no difference between using a view instead of a table.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18542 Visits: 14898
The biggest issue I've seen with using views is that sometimes a view may be accessing tables not needed for the results for that specific query or doing other operations that may not be needed for that specific query.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Bouke Bruinsma
Bouke Bruinsma
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 598
I have come across examples where views make it easier to introduce performance problems. Especially when columns are an outcome of a function or case statement etc. A view might be a good instrument to collect combined data that is often accessed but it hides the physical structure of the tables.
Recently I tuned a query that got data from view with a filter on a column that was actually an outcome of a calculation. Rewriting using only the tables got it speedy.

So I would say views are a good instrument in the hands of experts and require proper disclaimers for usage.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87521 Visits: 45272
John Deupree (5/2/2014)
Can someone comment on the performance implications of using views as opposed to the tables themselves.


Depends on how complex the views are. Simple views that just narrow and/or filter a table, fine. Couple of joins, probably fine. Lots and lots of joins, aggregation, views upon views, you're asking for performance problems. Partially from the complexity making it harder for the optimiser to find a good plan, partially from the time required to inline all the views and simplify and partially because the optimiser may decide to materialise an interim resultset due to the aggregations, sorting, etc.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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