Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

SQL - Derived Tables Expand / Collapse
Author
Message
Posted Tuesday, January 15, 2008 9:44 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, November 17, 2008 9:27 PM
Points: 81, Visits: 128
Comments posted to this topic are about the item SQL - Derived Tables

Thanks,
Prashant
Post #443400
Posted Tuesday, January 15, 2008 9:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 11, 2014 10:39 AM
Points: 18, Visits: 83
I try to use derived tables whenever possible, but I recently improved performance by changing a derived table to a temp table. It was large and joined on a non-index column so I found that a temp table with an index on the join fields.


Post #443405
Posted Wednesday, January 16, 2008 12:08 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, July 3, 2014 2:36 PM
Points: 631, Visits: 236
Great article.

We have been using the derived table as a great complex query design tool:

Break the complex query into derived tables, generally by the specific table's restrictions.

Then JOIN the individual derived table queries into the complex query.

BTW here id the fix for the original query:

SELECT C.CustomerID, C.CompanyName
, COUNT(O.OrderID) AS TotalOrders
FROM Customers C
LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID
AND O.OrderDate >= '19960101'
AND O.OrderDate < '19970101'
GROUP BY C.CustomerID, C.CompanyName

The last step is to evaluate for optimization.

A suggestion is no try and not use functions in the where clause as that can cause any available index to be skipped.

Andy



Post #443424
Posted Wednesday, January 16, 2008 12:18 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 29, 2014 3:18 PM
Points: 156, Visits: 367
"Theta Joins" also can be used.

For the given example in the article, the results should match for this query:

select c.*, o.*
from customers as c
left join orders as o
on c.customerid = o.customerid and year(o.orderdate) = 1996
--where
-- ...
Post #443426
Posted Wednesday, January 16, 2008 12:44 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:45 PM
Points: 35,342, Visits: 31,875
...and everything happens in memory instead of a combination of memory and disk.

...while a temporary table exists until the server is brought down and also it uses lot of disk space than a derived table in the temdb database.


You wrote a very good introduction to the use of Derived Tables... but you really need to check your resources on the two statements above....

First, Derived Tables are NOT memory only constructs... just like any query, if they run out of room in memory, they WILL make a thing called a "Working" table and you can frequently see them in the text version of execution plans. Guess where those bad boys live... you guessed it... memory if they fit and if they don't, TEMPDB!

Also, your information about Temp Tables living only on disk is dead wrong. Please read the following Microsoft provided URL...

http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k

... and pay particular attention to where it states "If memory is available, both table variables and temporary tables are created and processed while in memory (data cache). "

You also need to check Books Online for what the persistance of a Temp Table actually is... you DON'T need to bring the server down to get rid of a Temp Table. ;)

Don't let this dampen your writing spirit... you provided a really good intro to derived tables... I just had to correct your statements about temp tables. In fact, there are some occasions where a Temp Table will blow the doors off of derived tables... usually in a multi-table-joined update when aggragates are necessary. They work pretty well as a substitute for CTE's, too!


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #443430
Posted Wednesday, January 16, 2008 12:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 28, 2014 1:28 AM
Points: 14, Visits: 70
Derived tables are a great performance optimisation tool but always check because the derived table approach is not always the best and temporary tables are sometimes a necessary approach.

The derived table itself is just expanded into the main query (just like a view) this means that there are no statistics available for the derived table.

Statistics are held for the temporary table; you can also create indexes on temporary tables.

I use derived tables a lot, well, one hell of a lot to be honest and they are a good structure but also look at CTE's which is what I'm using more - they can be referenced more like a virtual table in your queries.

Tony.
Post #443435
Posted Wednesday, January 16, 2008 2:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 16, 2012 4:53 AM
Points: 2, Visits: 28
David A. Long (1/16/2008)
Great article.

BTW here id the fix for the original query:

SELECT C.CustomerID, C.CompanyName
, COUNT(O.OrderID) AS TotalOrders
FROM Customers C
LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID
AND O.OrderDate >= '19960101'
AND O.OrderDate < '19970101'
GROUP BY C.CustomerID, C.CompanyName

Andy


I think it also do the job:

SELECT C.CustomerID, C.CompanyName
, COUNT(O.OrderID) AS TotalOrders
FROM Customers C
LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID AND
YEAR(O.OrderDate) = 1996
--AND O.OrderDate >= '19960101'
--AND O.OrderDate < '19970101'
GROUP BY C.CustomerID, C.CompanyName

OR:

SELECT C.CustomerID, C.CompanyName, COUNT(O.OrderID) AS TotalOrders
FROM Customers C LEFT OUTER JOIN Orders O ON
C.CustomerID = O.CustomerID
WHERE YEAR(O.OrderDate) = 1996
GROUP BY ALL C.CustomerID, C.CompanyName
Post #443462
Posted Wednesday, January 16, 2008 3:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 3, 2012 9:35 AM
Points: 46, Visits: 58
I would tend to put some of the criteria in the FROM clause in a lot of cases rather than move straight to derived tables. Although in complex situations derived tables can help simplify things. But if the derived table is of particular value and reusable i will often put it in a view anyway.

pain_killer , the point about NOT using the Year() function is that it will make better use of any index.



Dan Bayley
affordable website design UK
Free Google and Yahoo sitemap generator
Post #443481
Posted Wednesday, January 16, 2008 4:02 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, September 22, 2014 9:30 AM
Points: 216, Visits: 1,369
I'm afraid I'm with Jeff Moden and Tony Rogerson on this.

I think this is a good example of needing to be careful and understand the implications of what you are saying, and to have covered your research well. There is plenty of info out there on this area from 'good' sources to allow you to draw a conclusion(s) that you can back up (right or wrong, subjectively of course ;) )

That said though, I would like to mirror Jeff's comments of not letting the replys dampen your spirit. You have put yourself forward for peer review and from my own experiences its a great way to gain experience and knowledge, and to improve, though it can seem quite painful along the route .
Post #443507
Posted Wednesday, January 16, 2008 5:37 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 15, 2014 5:31 AM
Points: 139, Visits: 4,617
And why not using this sentence:

SELECT C.CustomerID, C.CompanyName,
COUNT(O.OrderID) AS TotalOrders
FROM Customers C LEFT OUTER JOIN Orders O ON
C.CustomerID = O.CustomerID AND (YEAR(O.OrderDate) = 1996 OR O.OrderDate IS NULL)

GROUP BY C.CustomerID, C.CompanyName

Post #443547
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse