|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Monday, November 17, 2008 9:27 PM
Points: 81,
Visits: 128
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 3:40 PM
Points: 18,
Visits: 79
|
|
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.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Friday, October 19, 2012 1:44 PM
Points: 631,
Visits: 230
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: 2 days ago @ 5:10 PM
Points: 135,
Visits: 205
|
|
"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 -- ...
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 1:24 PM
Points: 32,913,
Visits: 26,806
|
|
...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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, March 09, 2012 3:05 AM
Points: 13,
Visits: 57
|
|
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.
|
|
|
|
|
Forum 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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, January 03, 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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, November 28, 2012 4:08 AM
Points: 160,
Visits: 1,006
|
|
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 .
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 1:35 AM
Points: 139,
Visits: 4,605
|
|
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
|
|
|
|