0

我有一种相当复杂(而且效率非常低)的方法来从大量时期(下面的代码)中获取利用率。

目前我运行了 8 周,返回数据需要 30 到 40 秒。

我需要定期运行 6 个月、1 年和 2 年,这显然需要大量时间。

有没有更聪明的方法来运行这个查询来减少表扫描的数量?我尝试了几种加入数据的方法,似乎都返回垃圾数据。

我试图尽可能多地评论代码,但如果有任何不清楚的地方请告诉我。

桌子尺寸:

[Stock]        ~12,000 records
[Contitems]    ~90,000 records

为清楚起见,伪代码:

For each week between Start and End:
    Get list of unique items active between dates (~12,000 rows)
        For each unique item
            Loop through ContItems table (~90,000 rows)
            Return matches
        Group
    Group
Return results

编码

DECLARE @WEEKSTART DATETIME; -- Used to pass start of period to search
DECLARE @WEEKEND DATETIME; -- Used to pass end of period to search
DECLARE @DC DATETIME; -- Used to increment dates
DECLARE @INT INT; -- days to increment for each iteration (7 = weeks)
DECLARE @TBL TABLE(DT DATETIME, SG VARCHAR(20), SN VARCHAR(50), TT INT, US INT); -- Return table

SET @WEEKSTART = '2012-05-01'; -- Set start of period
SET @WEEKEND = '2012-06-25'; -- Set end of period
SET @DC = @WEEKSTART; -- Start counter at first date
SET @INT = 7; -- Set increment to weeks

WHILE (@DC < @WEEKEND) -- Loop through dates every [@INT] days (weeks)
BEGIN
    SET @DC = DATEADD(D,@INT,@DC); -- Add 7 days to the counter

        INSERT INTO @TBL (DT, SG, SN, TT, US) -- Insert results from subquery into return table
        SELECT @DC, SUB.GRPCODE, SubGrp.NAME, SUM(SUB.TOTSTK), SUM(USED)

        FROM
        (
        SELECT STK.GRPCODE, 1 AS TOTSTK, CASE (SELECT COUNT(*) 
                                                           FROM ContItems -- Contains list of hires with  a start and end date
                                                           WHERE STK.ITEMNO = ContItems.ITEMNO -- unique item reference
                                                           AND ContItems.DELDATE <= DATEADD(MS,-2,DATEADD(D,@INT,@DC)) -- Hires starting before end of week searching
                                                           AND (ContItems.DOCDATE#5 >= @DC -- Hires ending after start of week searching
                                                                OR ContItems.DOCDATE#5 = '1899-12-30 00:00:00.000')) -- Or hire is still active
                                                     WHEN 0 THEN 0 -- None found return zero
                                                     WHEN NULL THEN 0 -- NULL return zero
                                                     ELSE 1 END AS USED -- Otherwise return 1

        FROM Stock STK - List of unique items

        WHERE [UNIQUE] = 1 AND [TYPE] != 4 -- Business rules
        AND DATEPURCH < @DC AND (DATESOLD = '1899-12-30 00:00:00.000' OR DATESOLD > DATEADD(MS,-2,DATEADD(D,@INT,@DC))) -- Stock is valid between selected week
        ) SUB
        INNER JOIN SubGrp -- Used to get 'pretty' names
        ON SUB.GRPCODE = SubGrp.CODE
        GROUP BY SUB.GRPCODE, SubGrp.NAME






END

-- Next section gets data from temp table
SELECT SG, SN, SUM(TT) AS TOT, SUM(US) AS USED, CAST(SUM(US) AS FLOAT) / CAST(SUM(TT) AS FLOAT) AS UTIL
FROM @TBL
GROUP BY SG, SN
ORDER BY TOT DESC
4

2 回答 2

2

我有两个建议。

首先,重写查询以将“select”语句从 case 语句移动到 from 子句:

SELECT @DC, SUB.GRPCODE, SubGrp.NAME, SUM(SUB.TOTSTK), SUM(USED)
FROM (SELECT STK.GRPCODE, 1 AS TOTSTK,
              (CASE MAX(Contgrp.cnt) -- Or hire is still active
                     WHEN 0 THEN 0 -- None found return zero
                     WHEN NULL THEN 0 -- NULL return zero
                     ELSE 1
                END) AS USED -- Otherwise return 1
        FROM Stock STK left outer join -- List of unique items
             (SELECT itemno, COUNT(*) as cnt
              FROM ContItems -- Contains list of hires with  a start and end date
              WHERE ContItems.DELDATE <= DATEADD(MS,-2,DATEADD(D,@INT,@DC)) AND -- Hires starting before end of week searching
                     (ContItems.DOCDATE#5 >= @DC OR -- Hires ending after start of week searching
                      ContItems.DOCDATE#5 = '1899-12-30 00:00:00.000'
                     )
              group by ITEMNO
             ) ContGrp
             on STK.ITEMNO = ContItems.ITEMNO
        WHERE [UNIQUE] = 1 AND [TYPE] != 4 AND -- Business rules
              DATEPURCH < @DC AND (DATESOLD = '1899-12-30 00:00:00.000' OR DATESOLD > DATEADD(MS,-2,DATEADD(D,@INT,@DC))) -- Stock is valid between selected week
       ) SUB INNER JOIN SubGrp -- Used to get 'pretty' names
       ON SUB.GRPCODE = SubGrp.CODE
 GROUP BY SUB.GRPCODE, SubGrp.NAME 

在这样做的过程中,我发现了一个可疑的地方。case语句在“ItemNo”级别操作,但分组是按“GrpCode”。因此,“Count(*)”实际上是在组级别返回总和。这是你的意图吗?

第二个是省去 WHILE 循环,如果你有多个星期的话。为此,您只需将 DatePurch 转换为适当的一周。但是,如果代码通常只运行一到两周,那么这种努力可能不会有太大帮助。

于 2012-06-25T13:25:51.153 回答
1

好吧,首先替换 WHERE 子句中的 DATEADD 函数。

你已经有了

SET @DC = DATEADD(D,@INT,@DC);

为什么不为删除日期声明另一个局部变量:

WHILE (@DC < @WEEKEND) -- Loop through dates every [@INT] days (weeks)
BEGIN
    SET @DC = DATEADD(D,@INT,@DC); 

    DECLARE @DeletionDate DATETIME =  DATEADD(MS,-2,DATEADD(D,@INT,@DC));

并在 case 语句中使用它:

CASE (SELECT COUNT(*) .... AND ContItems.DELDATE <= @DeletionDate ....

而且在外部where子句中......

然后你需要确保你已经正确地索引了你的表。

于 2012-06-25T10:56:29.537 回答