我有一個相當複雜(並非常低效)的方式來從大量的時間段(代碼如下)獲取利用率。更好的方法來計算利用率
目前我運行這個爲期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
如果您的日期只有日期,您可能會從代表儒略日期的整數列中獲得更好的性能;或取決於你查詢這些數據的多少,去完整[日期維度](http://www.amadeus-software.com/html/articles/date_dimension.html)可以付清 – AakashM