Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 28th, 2003, 04:25 AM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question when to use derived tables and temp tables

after reading some stuffs about derived tables and temp tables..

now i'm all confused when to use either of them..

based from your database programming experiences which of them performs better?

need your expertise.. thanks

-rhamille

Reply With Quote
  #2  
Old May 28th, 2003, 06:40 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
use derived tables whenever possible, i.e. if you can write the sql with derived tables, do so

rudy

Reply With Quote
  #3  
Old May 28th, 2003, 09:45 AM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
oh.. sorry what i mean was the difference in performance..

and someone also said that i can use a derived table inside a view..

would it be ok if i'll just make a view out of that derived table

so that i'll just use that view instead of including the sql statement for that derived table..

for example..

CREATE PROCEDURE [dbo].[ToPool]
(
@ordBy [varchar](4),
@sortBy [varchar](25)
)
AS
BEGIN
SELECT y.productCode, SUM(quantity * z.lastPurchasePrice) AS estimateCost, quoteDesc, quoteNo, poolDate, closingDate, emergency
FROM ( -- this is a derived table "y" right?
SELECT dbo.PoolLine.productCode, SUM(dbo.PoolLine.quantity) AS quantity, dbo.QuoteType.quoteDesc, dbo.Pool.quoteNo, dbo.Pool.poolDate, dbo.Pool.closingDate, NULL AS emergency
FROM dbo.Pool INNER JOIN
dbo.PoolLine ON dbo.Pool.poolID = dbo.PoolLine.poolID INNER JOIN
dbo.QuoteType ON dbo.Pool.quoteType = dbo.QuoteType.quoteType
GROUP BY dbo.PoolLine.productCode, dbo.QuoteType.quoteDesc, dbo.Pool.quoteNo, dbo.Pool.poolDate, dbo.Pool.closingDate
UNION ALL
SELECT TOP 100 PERCENT dbo.MRFLine.productCode, SUM(dbo.MRFLine.quantity) AS quantity, NULL AS quoteDesc, NULL AS quoteNo, NULL AS poolDate, NULL AS closingDate,emergency
FROM dbo.MRF INNER JOIN
dbo.MRFLine ON dbo.MRF.mrfID = dbo.MRFLine.mrfID
WHERE (dbo.MRF.statusID = 3) AND (dbo.MRF.projectID IS NULL)
GROUP BY dbo.MRFLine.productCode, emergency
UNION ALL
SELECT TOP 100 PERCENT dbo.MRF.projectCode AS productCode, SUM(dbo.MRF.quantity) AS quantity, NULL AS quoteDesc, NULL AS quoteNo, NULL AS poolDate, NULL AS closingDate,emergency
FROM dbo.MRF
WHERE (dbo.MRF.statusID = 3) AND (dbo.MRF.projectID IS NOT NULL)
GROUP BY dbo.MRF.projectCode, emergency) y INNER JOIN
( -- this is another derived table "z" which i joined to "y"
SELECT TOP 100 PERCENT x.productCode, MIN(ProductBrand.lastPurchasePrice) AS lastPurchasePrice
FROM ( -- derived table "x" i joined inside "z"
SELECT DISTINCT productCode, MAX(lastPurchaseDate) AS lastPurchaseDate
FROM ProductBrand
GROUP BY productCode) x INNER JOIN ProductBrand
ON x.productCode=ProductBrand.productCode AND x.lastPurchaseDate=ProductBrand.lastPurchaseDate
GROUP BY x.productCode) z ON y.productCode=z.productCode
GROUP BY y.productCode, y.quoteDesc, y.quoteNo, y.poolDate, y.closingDate, y.emergency
ORDER BY
CASE
WHEN @sortBy = 'y.productCode' THEN y.productCode
WHEN @sortBy = 'SUM(quantity * z.lastPurchasePrice)' THEN SUM(quantity * z.lastPurchasePrice)
WHEN @sortBy = 'y.quoteDesc' THEN y.quoteDesc
WHEN @sortBy = 'y.quoteNo' THEN y.quoteNo
WHEN @sortBy = 'y.poolDate' THEN y.poolDate
WHEN @sortBy = 'y.closingDate' THEN y.closingDate
END
RETURN
END
GO

-----

can i just make x, y and z as views and make a view out of them? and so that i can have a simple order by without having parameters for it? what will be the difference in performance?

any suggestions/comments? thanks..

rhamille

Reply With Quote
  #4  
Old May 28th, 2003, 10:54 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
derived tables and views should perform the same -- you asked about temp tables, and temp tables are different

i did not analyze your derived tables (the whole thing looks mighty complicated) but essentially, yes, you can usually just replace a derived table with a view

Reply With Quote
  #5  
Old May 28th, 2003, 12:52 PM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
oh aight... so like.. in essense it would be like nesting views right? would you recommend it then? well i didn't like the concept of temp tables..

i dunno.. i have to say this is my first big project.. and my paranoia for the speed of my program is really getting into me..

can you give me some tips maybe?

Reply With Quote
  #6  
Old May 28th, 2003, 01:04 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
recommend derived tables? yes, if you are 100% that the derived table will be used only once in that query -- otherwise, if it's used in more than one query, obvioulsy a view is better

tips? yeah

rule #1 -- design the tables properly

rule #2 -- forget about performance until you are sure your query is returning the right results

rule #3 -- see rule #1


Reply With Quote
  #7  
Old May 28th, 2003, 01:22 PM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
aight.. gee tnx man..

well databases are not my forte.. i just got stuck here for this project 1 man team so to speak hehehe.. i do both the programming and the database side.. but good for me coz m learning a lot of db stuffs.. i'll keep in mind everything that you have said...

o yeah another thing.. how many subqueries/joins do you think is enough? and how many levels of nested views should i take into consideration?

Reply With Quote
  #8  
Old May 28th, 2003, 01:58 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
if you find yourself doing more than two levels of nesting, see rule #1


Reply With Quote
  #9  
Old May 28th, 2003, 02:32 PM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
well.. i guess.. i was over normalizing the design.. hehehe.. can u giv me also tips on database normalization..

coz when i was normalizing it.. all i was thinking was to avoid the redunduncy of row data per table.. that's why i kept splitting my tables..

more than two levels? are joins considered as another nest level? does joining, lets say 3-5 tables, are bad design already?

Reply With Quote
  #10  
Old May 28th, 2003, 03:24 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
"avoid the redunduncy of row data per table" sounds right

don't worry about joins, they are efficient as long as you have indexed your join column

there are a number of links to good tutorials on my site:

http://r937.com/links.cfm?links=sql

Reply With Quote
  #11  
Old May 28th, 2003, 11:24 PM
rhamille rhamille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: pinoy
Posts: 15 rhamille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
aight.. tnx..

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > when to use derived tables and temp tables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.