Home > SQL Server Tips > Database Administration > Nested queries vs. temporary tables
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE ADMINISTRATION

Nested queries vs. temporary tables


Serdar Yegulalp
06.01.2004
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


There are two common approaches to working with large queries derived from other queries. One is to create a nested query, a SELECT within a SELECT, which derives the results of one query from another.

Another is to SELECT records into a temporary table, then perform another SELECT on the temporary table to further cull the results. This is a multi-step operation that requires creating, using and then dropping the temporary table.

The first approach is best where there is plenty of memory available to run fairly large queries and there is little chance of the query's complexity creating a timeout. Nested queries have the advantage of being run almost exclusively in physical memory for the sake of speed, unless the amount of memory needed to perform the query exceeds the physical memory "footprint" allocated to SQL Server.

Also, because of a nested query's structure, the SQL Server query optimizer will attempt to find the most optimal way to derive the results from the query. A temporary-table method may require several transactions, which is inherently slower. Temporary tables also have to be dropped at the end of the transaction manually, even if the query has timed out or failed in some other way.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Database Administration
Top load balancing methods for SQL Server
Performance implications of transaction log autogrowth in SQL Server
The keys to database backup protection for SQL Server
Understanding transparent data encryption in SQL Server 2008
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
Implementing SQL Server 2008 FILESTREAM functionality
Improving SQL Server full-text search performance
Using the OPENROWSET function in SQL Server
New replication features in SQL Server 2008 and what they mean to you

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


However, temporary tables have a few advantages that nested queries don't. Because using temporary tables for derived operations are multi-step operations, there are more opportunities for the user to be kept abreast of the progress of things. For instance, if the process is performed from an ASP page, with each step in the process being a separately-dispatched SQL statement, the user can be kept continually informed at what stage the whole thing is at. This is especially useful if several SELECT operations are needed to completely populate the temporary table, which can extend the total time needed to process a simple nested query way beyond the timeout limit. If raw speed is not absolutely of the essence, or if it can be ameliorated with continual user feedback, this is a good way to proceed.

A side note: Using transient permanent tables for temporary data is never a good idea. Permanent tables require writes to the transaction log, which incur a performance hit.


Serdar Yegulalp is the editor of the Windows 2000 Power Users Newsletter. Check out his Windows 2000 blog for his latest advice and musings on the world of Windows network administrators – please share your thoughts as well!


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts