5

以下查询返回正确的结果,但如何更快地获得相同的结果?

目标是通过汇总他们今天、本周、本月和季度的销售额来输出一个用于跟踪卖家进度的表格。

SellerID    Today                 ThisWeek              ThisMonth             ThisQuarter
----------- --------------------- --------------------- --------------------- ---------------------
1           400,00                700,00                900,00                900,00
2           950,00                1850,00               2650,00               2650,00

我的查询:

CREATE TABLE #sales(
    [Price] MONEY,
    [Date] DATE,
    [SellerID] INT
)

INSERT INTO #sales VALUES 
(100, '2012-01-01', 1),
(200, '2012-04-01',1),
(300, '2012-04-23',1),
(400, '2012-04-27',1),
(700, '2012-01-01', 2),
(700, '2012-01-02', 2),
(800, '2012-04-01',2),
(900, '2012-04-23',2),
(950, '2012-04-27',2)


SELECT 
SellerID AS SellerID,

SUM(CASE WHEN [Date] >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()),0) THEN [Price] END) AS Today,
SUM(CASE WHEN [Date] >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) THEN [Price] END) AS ThisWeek,
SUM(CASE WHEN [Date] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN [Price] END) AS ThisMonth,
SUM(CASE WHEN [Date] >= DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), 0) THEN [Price] END) AS ThisQuarter

FROM #sales
WHERE DATEPART(YEAR, [Date]) = DATEPART(YEAR, GETDATE()) 
GROUP BY SellerID

在更大的表上执行相同的查询时,这会变得很慢。仅删除 CASE 语句即可将执行时间缩短近 50%。

我怎样才能以更快、更有效的方式获得相同的结果?

4

3 回答 3

9

由于是星期五下午,我想我会扩大我对仓储的评论。即使您无法使用 SSAS 或任何其他 OLAP 完全探索多维数据集,您仍然可以进行自己的报表特定仓储。在你的情况下,我会建立一个新的数据库(我总是叫我的 DW,但世界是你的牡蛎),并创建 2 个模式 Fact 和 Dim(代表事实和维度)。在您的情况下,它需要 2 个表格,但您可能需要为“SellerID”添加另一个维度,具体取决于这是否需要进一步报告。

CREATE TABLE Dim.Date
(       DateKey     DATE NOT NULL,
        DayOfWeek   VARCHAR(20) NOT NULL,
        Day         TINYINT NOT NULL,
        Week        TINYINT NOT NULL,
        Quarter     TINYINT NOT NULL,
        Month       TINYINT NOT NULL,
        Year        SMALLINT NOT NULL
    CONSTRAINT PK_Dim_Date_DateKey PRIMARY KEY (DateKey)
)
CREATE TABLE Fact.Sales
(       DateKey     DATE NOT NULL,
        SellerID    INT NOT NULL,
        Sales       INT NOT NULL,
        Amount      MONEY NOT NULL,
    CONSTRAINT PK_Fact_Sales PRIMARY KEY (DateKey, SellerID),
    CONSTRAINT FK_Fact_Sales_DateKey FOREIGN KEY (DateKey) REFERENCES Dim.Date
)

假设数据不会回溯,您可以使用这样的过程来按计划作业填充您的仓库:

DECLARE @MaxDate DATE
SELECT  @MaxDate = DATEADD(DAY, 1, MAX(DateKey))
FROM    Fact.Sales

INSERT INTO Dim.Date
SELECT  DATEADD(DAY, Increment, @MaxDate), 
        DATENAME(WEEKDAY, DATEADD(DAY, Increment, @MaxDate)), 
        DATEPART(DAY, DATEADD(DAY, Increment, @MaxDate)),
        DATEPART(WEEK, DATEADD(DAY, Increment, @MaxDate)),
        DATEPART(MONTH, DATEADD(DAY, Increment, @MaxDate)),
        DATEPART(QUARTER, DATEADD(DAY, Increment, @MaxDate)),
        DATEPART(YEAR, DATEADD(DAY, Increment, @MaxDate))
FROM    (   SELECT  ROW_NUMBER() OVER(ORDER BY Object_ID) - 1 [Increment]
            FROM    Sys.Objects
        ) obj
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    Dim.Date
            WHERE   Date.DateKey = DATEADD(DAY, Increment, @MaxDate)
        )


INSERT INTO Fact.Sales
SELECT  [Date], SellerID, COUNT(*), SUM(Price)
FROM    LiveDatabase..Sales
WHERE   [Date] >= @MaxDate
GROUP BY [Date], SellerID

这将为您留下以下查询来生成您的报告

SELECT  SellerID,
        SUM(CASE WHEN Today.DateKey = Date.DateKey THEN Amount ELSE O END) [Today],
        SUM(CASE WHEN Today.Week = Date.Week THEN Amount ELSE O END) [ThisWeek],
        SUM(CASE WHEN Today.Month = Date.Month THEN Amount ELSE O END) [ThisMonth],
        SUM(CASE WHEN Today.Quarter = Date.Quarter THEN Amount ELSE O END) [ThisQuarter],
        SUM(CASE WHEN Today.Year = Date.Year THEN Amount ELSE O END) [ThisYear]
FROM    Fact.Sales
        INNER JOIN Dim.Date
            ON Date.DateKey = Sales.DateKey
        INNER JOIN Dim.Date Today
            ON Today.DateKey = CAST(GETDATE() AS DATE)
            AND Today.Year = Date.Year
GROUP BY SellerID

如果有的话,它看起来比原始查询更复杂,但是在线数据库增长得越多,您就会看到更多的好处。我做了一个SQL Fiddle来展示优势,它用 10000 条随机销售记录填充实时数据,然后创建一个仓库(构建模式可能需要几秒钟)。您应该注意到仓库上查询的执行时间明显更快(c.20x)。第一次运行时它可能不会快 20 倍,但是一旦为两个查询缓存了查询计划,仓库查询就会始终快 20 倍(无论如何对我来说都是如此)。

于 2012-04-27T16:53:28.810 回答
2

也许保留数据的非规范化版本?

例如:http ://sqlfiddle.com/#!3/300a5/2

select 
    *
    ,DATENAME(day, [date]) as day
    ,DATENAME(month, [date]) as month
    , DATENAME(year, [date])  as year
    ,DATENAME(quarter, [date]) as quarter
into deNormalised 
from #sales

然后您可以运行以下查询:

select 
    year
    ,sum(price)
from 
    deNormalised
where 
    quarter = 1
group by 
    year

比较不同年份的第一季度

显然,这意味着您必须制定一个计划来维护数据的非规范化版本。您可以通过更新或每小时触发来执行此操作。

您也可以尝试将最新数据添加到非规范化结果中。这样您只会对今天创建的行进行缓慢的处理。

编辑:我不知道仅使用 DATENAME 函数是否会提高使用现有结构的性能。

于 2012-04-27T15:10:57.083 回答
0
select 
     SellerID
    ,sum(case when [Date]=getdate() then [Price] else 0 end) as Today
    ,sum(case when datepart(week,[Date])=datepart(week,getdate()) then [Price] else 0 end) as ThisWeek
    ,sum(case when datepart(MONTH,[Date])=datepart(month,getdate()) then [Price] else 0 end) as ThisMonth
    ,sum(case when datepart(QUARTER,[Date])=datepart(QUARTER,getdate()) then [Price] else 0 end) as ThisQUARTER
from #sales
Group by SellerID
于 2016-11-01T21:45:14.063 回答