SQL,FROM,very,usefull,technique,which,reduce,
Quick Search for:  in language:    
SQL,FROM,very,usefull,technique,which,reduce,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 29,142 lines
 Jobs: 442 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for SQL
Access 2000 Tabular Form - Building a Query Command Function
By David Nishimoto on 8/23


sp_RestoreDatab ase
By Shane Lively on 8/23


fnCharPad
By Shane Lively on 8/23


Link_SQLServer_ Oracle
By Daniel G. Menendez on 8/20


SQL Strip Alpha/Numeric
By Jay Gardner on 8/19


how to apply a filter to an Access 2000 form by using a combo box as a filter parameter
By David Nishimoto on 8/16


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

More Efficient Queries using Vitual Tables!!

Print
Email
 
VB icon
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

Users have accessed this code 7039 times.
 
 
     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.
 
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 Vi
--     tual Tables!!
-- Description:This is a very usefull te
--     chnique which can reduce your query time
--     s exponentially on SQL Server databases.
--      A Virtual Table is a Select Query refer
--     enced like a table within the FROM claus
--     e of your select statement. By using a V
--     irtual Table, you will be able to have a
--      much more efficient query than if you w
--     ere to use a Temp Table or an IN (SELECT
--     ...) statement in your WHERE clause. Try
--      testing this out on large data sets, an
--     d 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/xq/ASP/txtCod
--     eId.283/lngWId.5/qx/vb/scripts/ShowCode.
--     htm--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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:MisterD
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/11/2001 3:16:30 AM:Thierry 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/11/2001 3:18:57 AM:Thierry 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/13/2001 12:41:39 PM:Chad 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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/9/2002 4:00:58 PM:Dustin
This code will speed up the writing of 
complicated query's but it takes longer 
to run then selections with the join 
statement.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.