| Thomas 的个人资料Thomas Kejsers Blog日志列表网络 | 帮助 |
|
2006/7/10 Doing FIRST and LAST aggregates in SQL Server 2005Users of Microsoft Access may be familiar with the aggregation functions FIRST and LAST. Basically, what you want from these aggregates is to scan the tables in a sorted order. The first or last value encountered in each group is send to the output - much like the existing SQL Server MIN/MAX function.
Let me illustrate with some pseudo code:
SELECT Col , FIRST(Value) AS FirstV , LAST(Value) AS LastV , SUM(Value) AS SumV FROM Table GROUP BY Col
On a table with these rows:
...You want this output:
Notice how there in an implicit assumption of a row ordering. You probably want some sort of identity column to order your rows. Implicitly - this is a very ISAM way of looking at the world. Now, how do we do this inside SQL Server?
Lets make some test data based on AdventureWorksDW:
SELECT s.*, IDENTITY(int, 1,1) AS IDINTO #AggFROM dbo.FactInternetSales sCROSS JOIN (SELECT TOP 30 * FROM sys.objects) scaleUpFactor
CREATE UNIQUE INDEX IX_FirstLast ON #Agg (ProductKey, ID, OrderQuantity)
Well... My colleage Jesper Rasmussion came up with a brilliant answer (try this on the testdata from above):
SELECT I.ProductKey, F.OrderQuantity AS FirstQuantity , L.OrderQuantity AS LastQuantity , I.SumQuantity AS SumQuantity FROM (SELECT ProductKey , min(ID) AS FirstID , max(ID) AS LastID , sum(OrderQuantity) AS SumQuantity FROM #Agg GROUP BY ProductKey) I INNER JOIN #Agg F ON F.ID = I.FirstID AND F.ProductKey = I.ProductKeyINNER JOIN #Agg L ON L.ID = I.LastID AND L.ProductKey = I.ProductKey
This query has some very interesting properties. If you check the executions statistics you will see that SQL Server only does very few I/O requests in the index created (not more than 10-20% more than it takes for a full scan - probably around log(size(#agg) ). Since all rows HAVE to be visited to answer the requiest - this means that the query is very efficient.
Now, if only we could make a first and last aggregate that does just one table scan... :-)
How fast can SQL Server 2005 possibly beI am currently making a relatively deep (read: nerdy) study of how to optimally load a huge data warehouse. Based on my experience with ETL tools, SQL tuning and index optimization I know that there are several approaches one may take when loading a billion row data warehouse with million row dimension tables. However, very few of them are optimal or even viable... On question that must be answered to find the optimal stratagy is the following: Let us assume we have an optimally configured SQL Server system (all sp_configure settings and trace flags optimized for our hardware). Futhermore, assume we have the following:
Now, let us run these three simple statements against our warehouse database:
These are (simplified) version of the queries used to respectively make key lookups, type 1 and type 2 dimension changes Assume the following about the execution of the above statements
For our warehouse architecture we can consider two viable scenarios:
Now my question is: How fast can these statements be if we have the best software, tuning and hardware available?...
In a later post I will explore a non-naïve approach which i consider optimal for dimension loading for dimension of any size. |
|
|