r/SQLServer • u/ScaryDBA Microsoft MVP • May 14 '18
A View Will Not Make Your Query Faster
https://www.scarydba.com/2018/05/14/a-view-will-not-make-your-query-faster/8
May 14 '18
Well a view is just a select. o nope it won't get faster. Unless you have a very large chunk of sql and a slow connection to the server it saves uploading the data.
However an indexed view. I have seen speedup certain thing 10000x or more because the output of a group by can be precomputed for every row in a view.
0
u/ScaryDBA Microsoft MVP May 15 '18
Agreed. Materialized views can radically improve performance. But they're not views, they're materialized views.
1
u/goblando May 15 '18
Another beautiful one is "instead of" triggers for insert update and delete on the view. If you are a shop that doesn't use stored procs for this or you have legacy code that is never allowed to be rewritten, you can actually take a table that is getting out of hand and refactor it correctly and then replace the original table with a view. The software never knows the difference and now they can slowly migrate away.
5
u/coggsa May 15 '18
"I backup my tables daily into individual tables with the dates on them and then manipulate a view that unions all the tables together so they can be in one fast and easy to access view. Whats so hard to understand?"
No. Just fucking no.
"Its slower than I thought, and it seems filters dont make it faster. It worked perfectly when we first implemented. You must have changed something."
Oh my fucking... Just go away.
1
u/ScaryDBA Microsoft MVP May 15 '18
Good gosh, where is that?
3
u/coggsa May 16 '18
Our SQL experts in head office. They don't understand that "Table Scan + Multiple Years of Date = Sssslllllooooooowwww"
Cant get them to understand how doing a table scan on 10,000 rows is not an "exhaustive test" for 3 years of data, 143 million rows from 1089 unioned tables. They also don't know why adding a "where filter" is slower, especially when they string match with "like" to eliminate some rows from the view, outside the view, when its being used as a subselect.
1
u/ScaryDBA Microsoft MVP May 16 '18
Ouchie. Seriously ouchie.
Show 'em the execution plan?
2
u/coggsa May 17 '18
Have been dismissed as "that guy" from the regional office.
The number of times I have been told things are "as designed" or "lower priority than saleable features" is depressing. Apparently, "Technical Debt" does not apply to our development methods.
Shocking that we have bugs and performance issues.
3
u/ScaryDBA Microsoft MVP May 17 '18
Technical debt is a killer. I face it at work. Hell, I've been the monster to introduce it in the past. Getting people convinced to work on it is hard. Usually one of two things happens. Either the debt prevents you from introducing a new feature, or the pain becomes noticeable to paying clients who call you out on it. Either of those usually inspires people to fix it. However, just fixing it because it's a good thing... yeah, doesn't happen often.
→ More replies (1)
4
May 14 '18 edited May 14 '18
Related, but on procs not views...
Maybe somebody can explain for me: I've often found that a proc does make my query run faster. Never mind that a properly parametrized query should still get properly analyzed and cached just like a proc. What gives?
-1
u/antennarex May 14 '18
Stored procedures are compiled. This allows for execution plan optimizations that don't have to be re-evaluated every time the proc is executed (unless you mark the proc as "with recompile").
1
May 14 '18
But I thought those execution plan optimizations were similarly provided by the plan cache?
I would assume the only difference after the first run (since the parameterized query is lazy-optimized on first run while the proc is optimized at compile-time) would be the time taken to hash the query and find it in the query plan cache.
1
May 14 '18
[removed] — view removed comment
2
May 14 '18
... yeah, that would make more sense.
I'm just confused, then, why I keep getting developers on my team saying "Hey, I fixed the performance problem when I replaced the query with a proc". Maybe it was just the fact that this replaces the cached plans with fresh ones that solved it.
2
May 14 '18
[removed] — view removed comment
1
May 14 '18
No, I've seriously been getting change scripts going to prod that are literally replacing a view with a proc with the exact same body and the product owner saying "just push the change, the performance on the test environment is way better with that".
The DBA is an old-school proc-lover and so doesn't back me up when I say "this seems wrong".
1
u/ScaryDBA Microsoft MVP May 15 '18
Well... replacing a view with a proc... yeah, that could lead to improved performance, depending on lots of stuff around it, but the proc is going to get parameter sniffing and not have to go through simplification and all the stuff around when call a view. Again, without details, just stabbing vaguely in the dark, but based on the rough outline you've provided, I can see places where it would work.
1
u/antennarex May 14 '18
It's my understanding that the compilation takes place the first time the proc is executed, not at the time it is created. Hence the ability to use sp_recompile to mark an existing proc for recompilation the next time it's run. Is that not the case?
0
May 15 '18
[deleted]
1
u/antennarex May 15 '18
They are not compiled to native machine code, but Microsoft uses the language "compiled" quite frequently in their documentation to refer to building and optimizing the query plan.
Directly from the official documentation:
When a procedure is compiled for the first time or recompiled, the procedure’s query plan is optimized for the current state of the database and its objects. If a database undergoes significant changes to its data or structure, recompiling a procedure updates and optimizes the procedure’s query plan for those changes. This can improve the procedure’s processing performance.
Of course you're not going to get magic benefits over a series of normal queries, but it has it's advantages, such as decoupling logic in the database from the application tier and the ability to perform operations on data without granting direct permission to the underlying table structures.
0
6
May 14 '18 edited May 14 '18
[removed] — view removed comment
1
u/ScaryDBA Microsoft MVP May 15 '18
I see a lot of people using the terms parameter and local variable interchangeably. It's the local variables that can lead to different performance than parameters. Not saying better or worse because it really is situational, but it's one that comes up a lot.
1
u/ScotJoplin May 14 '18
Maybe you want to explain that a little more with when ad-hoc query plans can be re-used. Only safe plans are re-used with default settings. You’d have to enable forced parameterisation for all ad-hoc plans to be able to be re-used.
1
3
May 14 '18
[removed] — view removed comment
1
u/coggsa May 15 '18
Nope. Stored Procs are compiled, and get optimised AFAIK. Views just get run each time.
1
May 15 '18
[removed] — view removed comment
1
u/coggsa May 15 '18 edited May 15 '18
Sorry, after writing a rebuttal... I noticed u/Voldemorte said "parameterised query."
Been arguing about SSRS reports pulling data from Excel docs all day. And the concept of "Reporting Groups" not being the same as "automatically fix all that." Brain fried. My bad.
2
May 14 '18
A normal view won't, but a materialized view often will, especially if you're doing aggregations. That said, don't just go running around building materialized views everywhere, because you're storing that data and you take a processing hit if the underlying data changes a lot.
3
May 14 '18
How often can you even make a materialized view? The requirements for schemabinding are so onerous I've never had a view I wanted to slap an index onto and had it actually work for me.
1
u/Cal1gula Database Administrator May 14 '18
Literally once in 8 years of working with SQL that I can recall.
0
May 14 '18
[deleted]
2
u/ScotJoplin May 14 '18
If you care to consider the processing overhead of permitting outer joins in indexed views you’ll see that it’s a pretty smart decision. If outer joins were permitted they wouldn’t exist at all. If you don’t then every change requires that section of the query to be processed (With the possibility of one to many joins) just to update the view. Then you have that on a table that gets a bunch of updates and voila your server is dead. No thanks.
0
May 14 '18
[deleted]
1
u/ScotJoplin May 15 '18
You could probably fake it with some effort but almost certainly with hideous performance. It would always force a scan of the outer side of you can. I might even see if I can get that working just for kicks...
13
u/dgillz May 14 '18
Performance is not what views are for. It is usually for reporting purposes and often includes data from multiple tables. Views are very important to any SQL pro that knows his/her database.