BorderLayoutBoxedLayoutOpenLayoutMaximum textMedium textSmall text


Register
Tuesday, September 07, 2010
Search ResultsMinimize
1Partial Aggregation -Relevance: 1016

In some of my past posts, I've discussed how SQL Server implements aggregation including the stream aggregate and hash aggregate operators.  I also used hash aggregation as an initial example in my introductory post on parallel query execution.   In this post, I'll look at a partial aggregation.  Partial aggregation is a technique that SQL Server uses to optimize parallel aggregation.  Before I begin, I just want to note that I also discuss partial aggregation in Inside Microsoft SQL Server 2005: Query Tuning and Optimization.  (See the bottom of page 187.)

Let's begin with a simple scalar aggregation example.  Recall that a scalar aggregate is an aggregate without a GROUP BY clause.  A scalar aggregate always produces a single output row.

CREATE TABLE T (A INT, B INT IDENTITY, C INT,
http://statisticsio.com/Feeds/SQLBlogfeeds/tabid/60/Default.aspx -1/18/2008 1:12:00 PM

2Recursive CTEs -Relevance: 1001
One of the most important uses of CTEs is to write recursive queries.  In fact, CTEs provide the only means to write recursive queries.  As I noted last week, there are several excellent CTE examples, including recursive CTE examples, in Books Online.  I'll once again start with the examples from Books Online.  To run these examples, you'll need to install the Adventure Works Cycles OLTP sample database.

Recursive CTEs always follow the same pattern.  The body of the CTE is a UNION ALL query that combines one or more anchor sub-selects which seed the result set and one or more recursive sub-selects which generate the remainder of the result set.  The recursive sub-selects reference the recursive CTE itself.

Let's look at a simple example which computes a list of all employees and their level within the organization.  Computing the level with the organization can only be done using a recursive CTE as it requires counting the number of joins required to reach each employee:

WITH DirectReports(ManagerID, EmployeeID, EmployeeLevel) AS
(
    SELECT ManagerID, EmployeeID, 0 AS EmployeeLevel
    FROM HumanResources.Employee
    WHERE ManagerID IS NULL
    UNION ALL
&n
http://statisticsio.com/Feeds/SQLBlogfeeds/tabid/60/Default.aspx -10/25/2007 1:57:00 PM

sys.bookssys.booksMinimize





Print  

Copyright 2006 by Statistics IO, My SQL Server Blog