Important alert: (current site time 7/15/2013 8:07:10 PM EDT)
 

VB icon

More Efficient Queries using Vitual Tables!!

Email
Submitted on: 3/2/2001 6:24:50 PM
By: David Drake 
Level: Intermediate
User Rating: By 4 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 24414
 
     This is a very usefull technique which can reduce your query times exponentially on SQL Server databases. A Virtual Table is a Select Query referenced like a table within the FROM clause of your select statement. By using a Virtual Table, you will be able to have a much more efficient query than if you were to use a Temp Table or an IN (SELECT...) statement in your WHERE clause. Try testing this out on large data sets, and you will see a significat performance gains.

I know these are simple examples that can easily be replaced with inner joins, but as you run into querying complex table relationships this will be a handy tool.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: More Efficient Queries using Vitual Tables!!
-- Description:This is a very usefull technique which can reduce your query times exponentially on SQL Server databases. A Virtual Table is a Select Query referenced like a table within the FROM clause of your select statement. By using a Virtual Table, you will be able to have a much more efficient query than if you were to use a Temp Table or an IN (SELECT...) statement in your WHERE clause. Try testing this out on large data sets, and you will see a significat performance gains.<BR><BR>
I know these are simple examples that can easily be replaced with inner joins, but as you run into querying complex table relationships this will be a handy tool.
-- By: David Drake
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=283&lngWId=5--for details.--**************************************

EXAMPLE 1 (Not using a Virtual Table):
 SELECT *
 FROM TableA
 WHERE ID IN (SELECT ID FROM TableB)
EXAMPLE 2 (Using a Virtual Table):
 SELECT A.*
 FROM TableA A,
(SELECT ID
FROM TableB) AS B
WHERE A.ID = B.ID
EXAMPLE 3 (Nesting Virtual Table):
 SELECT A.*
 FROM TableA A,
(SELECT ID
FROM TableB T,
(SELECT cID
FROM TableC) As C
WHERE T.ID = C.cID) AS B
 WHERE A.ID = B.ID


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/6/2001 2:44:18 PMMisterD

Not sure what you mean by larger datasets, but I've tried on two datasets (200,000+ rows and 2,000,000 plus rows) and your virtual table method above was actually 30+ seconds slower on the 2,000,000 row dataset, and about the same for the smaller one.
(If this comment was disrespectful, please report it.)

 
6/11/2001 3:16:30 AMThierry Gergaud

I think your tip is good, but when I try to use it, it doesn't seem to work. Do you know if it work properly with an Oracle server ? Regards.
(If this comment was disrespectful, please report it.)

 
6/11/2001 3:18:57 AMThierry Gergaud

I think your tip "queries using virtual tables" is good. But when I try it, it doesn't seem to work. Do you know if it works properly with an Oracle server ? Regards.
(If this comment was disrespectful, please report it.)

 
6/13/2001 12:41:39 PMChad M. Kovac

When I verify my SQL, it verified as OK, but then gives me an error near ',' in my sql statement:
SELECT txtPlan, txtRegionDesc AS RegionDesc,
numVolume AS decVolume, StatusCode AS txtStatus,
PCAccVol AS InitFace
FROM dbo.AgentProduction A,
(SELECT *, txtFilterName AS FilterName
FROM ck_rpt_PLANCODES Tx,
(SELECT *, age_range AS AgeRange,
subtotal_range AS SubtotalGroup,
rgedesc AS RangeDesc
FROM ck_Age_Descriptors) AS C
WHERE Tx.txtFilterName = C.Report) AS B
WHERE A.txtPlan = B.TxtPlanAccumName


Can you see what the deal is with this?
(If this comment was disrespectful, please report it.)

 
4/9/2002 4:00:58 PMDustin

This code will speed up the writing of complicated query's but it takes longer to run then selections with the join statement.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.