473,785 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2177
jc (jc**********@o ptusnet.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****@sommarsk og.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
3728
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 idea is: When people check the check boxes in HTML page for the PDF files, it will transfer the files' name to ASP page. Then, it will attach it in the email.
6
2018
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 stands supports syntax highlighting and outlining of functions, classes and their methods.] When I took over the project in June it was for the purpose of adding outlining facilities for JavaScript written in an OO style with classes and so on. I...
121
10180
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
17
4229
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 Set rs = Me.RecordsetClone rs.Find "=" & lngContractID If Not rs.EOF Then Me.Bookmark = rs.Bookmark I must site the Heisenberb Uncertainty Principal here, as it
0
2076
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 sub-report prints detail records. Typically if the detail records does not fill up the entire page, then the Totals line is printed immediately after the last detail record. What I want to do is to continue to print "dummy" records repeatedly after...
1
2766
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 have a table containing records for each job done, the records contain date, employee name, job done (a code representing the type of job), cost code (another code), regular hours, and overtime hours. The tricky part is that more than one job can...
53
4758
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, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
65
3929
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
2084
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++) {
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.