473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can you optimize duplicated work in views?

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
Jul 20 '05 #1
1 1382
malcolm (ch********@yahoo.com) writes:
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:


I ran this query in Northwind:

select OrderID, SUM(UnitPrice), SUM(Discount),
SUM(UnitPrice) + SUM(Discount)
from [Order Details]
group by OrderID

When I looked at the query plan, it appears to compute the first two sums
in a stream aggregate, and then computed the last sum as a sclar value,
being the sum of the two sums.

However, this may not at all be applicable to your actual query, since
your expressions may be more complex and not lend themselves to
arithmetic simplifications.

In general, though, I think you can trust the optimizer to do the
right thing. Rewriting the query in steps as you suggested, can just
as well make matters worse, since you could confuse the optimizer.
(Or even worse confuse yourself, so that you get incorrect results.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mjuricek | last post by:
I'm having some problems to optimize my stored procedure (select statement with joins) What I'm trying to do is calculate total work. My situation: I have 3 tables I'm using -Input (char...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
19
by: octangle | last post by:
This code is attempting to find records that have a RegJrnID that does not occur more than one time in the table. The reason that I want to find records with non-duplicated RegJrnID values is to...
13
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right...
2
by: orenlevy1 | last post by:
Hi Everyone. I have a problem that I could not figure out what to do with it. I have a couple of tables and views. All have the same unique ID. When a user try to do a search on our web site...
2
ADezii
by: ADezii | last post by:
Create Indexes on all Columns used in 'ad hoc' Query Joins, restrictions, and sorts (Jet already creates Indexes for Enforced Relationships). Use Primary Keys instead of Unique Indexes wherever...
6
by: Ryan Liu | last post by:
Hi, If I have tens of thousands DataRow in a DataTable and allow the end user to pick any DataColumn(s) to check for duplicated lines, the data is so large, is there a better API, algorithm can...
0
by: Edoardo | last post by:
thanks guys, I was hoping to be able to get the info from some method on oracle's own jdbc implementation classes, as both suggestion to use these view imply another trip to the db from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.