This one's kind of hard to explain, so I've opted to post a simplified
version of our view that prompted me to ask this question: The
question is re-asked after the view...
create view MainView (
PrimaryKeyID,
SubTotal1,
SubTotal2,
GrandTotal
)
as
select t.PrimaryKeyID,
sum(t1.Total),
sum(t2.Total),
sum(t1.Total) + sum(t2.Total)
from SomeTable t
join CalculationTable t1 on ...
join AnotherCalculationTable t2 on ...
Notice in the 3rd column called "GrandTotal" how it calls the function
"sum" two more times. Common sense tells me that this is not
necessary. in our case it's orders of magnitude worse... Is the query
optimizer smart enough to only call these sums once per row in
"SomeTable"? Common sense tells me that if we were to break the views
apart into two views it would avoid this ineffeciency:
create view InnerView (
PrimaryKeyID,
SubTotal1,
SubTotal2
)
as
select t.PrimaryKeyID,
sum(t1.Total),
sum(t2.Total)
from SomeTable t
join CalculationTable t1 on ...
join AnotherCalculationTable t2 on ...
create view OuterView (
PrimaryKeyID,
SubTotal1,
SutTotal2,
GrandTotal
)
as
select iv.PrimaryKeyID,
iv.Total1,
iv.Total2,
iv.Total1 + iv.Total2
from InnerView
Notice how it appears that we've tricked the optimizer into thinking
there are less operations. So my question is how does views handle
this situation? Does the optimizer treat both version the same? Or is
one faster than the other? Or is there another, faster way? Does
adding levels of views slow down things, or are views simply like
macros and get removed when compiled (I think I've read the latter is
true actually)
Thanks,
Dave