473,378 Members | 1,470 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,378 software developers and data experts.

Using the same View repeatedly

jc
Hi.
A question I have is with regard to the use of views with SQL2000. If
I have a view called "A_view" and used in the following manner;

----------------
SELECT ...
FROM A_View
WHERE ....

UNION

SELECT ....
FROM A_View
WHERE .....
-----------------

is the view computed twice? Ideally if the view is computationally
expensive I would rather it was only computed once.
Also this would be preferred for data consistency.

Is there a way to ensure the view is only computes once?

Regards JC.......
Jul 23 '05 #1
2 2150
jc (jc**********@optusnet.com.au) writes:
A question I have is with regard to the use of views with SQL2000. If
I have a view called "A_view" and used in the following manner;

----------------
SELECT ...
FROM A_View
WHERE ....

UNION

SELECT ....
FROM A_View
WHERE .....
-----------------

is the view computed twice? Ideally if the view is computationally
expensive I would rather it was only computed once.
Also this would be preferred for data consistency.

Is there a way to ensure the view is only computes once?


You can never be sure what the optimizer is up to, but you are correct
to assume that the view culd be evaluated twice for this type of query.
And if data is being inserted/modified while the query is running, the
two evaluiations could give different result.

To see how the optimizer computes the query, you can run the query in
Query Analyzer. You can just press Ctrl/L or select Query->Display
Estimated Execution Plan. Or you can press Ctrl/K and then run the
query to see the actual plan.

One way to avoid that the view is evaluated twice is to select the
result into a temp table, and then use that table in the query.

However this may have detrimental effect on performance. You see, the
when running the UNION query, SQL Server may never compute the full
view at all. Basically, the optimizer expands the view as a macro,
and applies the WHERE clause to that. As long the result is the same,
the optimizer may rearrange how the query is computed. So, theoretically,
the two parts in the UNION could have very different plans that only
computed a subset of the view.

The only way to find out what's the best is to benchmark.

--
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 23 '05 #2
Using the same table in two SELECTs of a UNION expression is usually
bad programming. You can almost always reduce it to:

SELECT [DISTINCT] ...
FROM Foobar
WHERE ...
OR ...;

When you get to a product with CTE (Common Table Expressions), you can
use a WITH clause to materialize the VIEW.

But having said all of this, you have to trust the optimizer. And most
optimizers are bad about UNION, INTERSECT and EXCEPT operations because
they are not that common.

Jul 23 '05 #3

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

Similar topics

15
by: tabonni | last post by:
Hi I try to grab the checked files from HTML page and then send those PDF files as attachments. It can just send email, there are no PDF files attached. Can anybody point out my error? My...
6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
0
by: Andrew | last post by:
Hi All: I have a main report that calls a sub-report. The sub-report is sorted by MemberID. The MemberID Footer Section of the sub-report prints a Totals line. The Detail Section of the...
1
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
65
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.