2015-10-14 62 views
1

我有一個包含18000000行(TABLE_A)的表格。我必須每15分鐘執行一次存儲過程,以從TABLE_A中選擇數據,進行一些計算並將結果插入到另一個表(TABLE_B)中。 TABLE_A的這個值來自包含不同設備讀數的文件。因此,桌子會越來越大。創建包含上百萬行的表格的視圖

這是文件內容的示例:

'2015-10-01 11:00:00.000';'par01';1 
'2015-10-01 12:00:00.000';'par02';1 
'2015-10-01 12:00:00.000';'par03';1 

我認爲這是更好地插入來自文件到另一個表(TABLE_C)的新值,並用該值進行計算TABLE_C。我想要一個查詢,當某個變量達到一個月的數據時,將大於一個月的行插入TABLE_A(對於我來說,一個月就足以進行計算)。因此,在TABLE_C中只有一個月的數據,計算的執行速度將比使用18000000行的速度更快。

這是表中的一個簡單的例子:

DECLARE @TABLE_C(timestamp datetime, parameter char(5), value int) 
DECLARE @TABLE_A(timestamp datetime, parameter char(5), value int) 

INSERT @TABLE_C values 
('2015-10-01 11:00:00.000','par01',1), 
('2015-10-01 12:00:00.000','par01',1), 
('2015-09-01 11:03:00.000','par01',2), 
('2015-09-01 12:03:00.000','par01',2), 
('2015-08-01 11:06:00.000','par01',3), 
('2015-08-01 12:06:00.000','par01',3), 

('2015-09-01 11:00:00.000','par02',1), 
('2015-09-01 12:00:00.000','par02',1), 
('2015-08-01 11:03:00.000','par02',2), 
('2015-08-01 12:03:00.000','par02',2), 
('2015-07-01 11:06:00.000','par02',3), 
('2015-07-01 12:06:00.000','par02',3), 

('2015-10-01 11:00:00.000','par03',1), 
('2015-10-01 12:00:00.000','par03',1), 
('2015-09-01 11:03:00.000','par03',2), 
('2015-09-01 12:03:00.000','par03',2), 
('2015-08-01 11:06:00.000','par03',3), 
('2015-08-01 12:06:00.000','par03',3) 

結果必須是財產以後像它:

INSERT @TABLE_C values 
('2015-10-01 11:00:00.000','par01',1), 
('2015-10-01 12:00:00.000','par01',1), 


('2015-09-01 11:00:00.000','par02',1), 
('2015-09-01 12:00:00.000','par02',1), 


('2015-10-01 11:00:00.000','par03',1), 
('2015-10-01 12:00:00.000','par03',1) 

INSERT @TABLE_A values 
('2015-09-01 11:03:00.000','par01',2), 
('2015-09-01 12:03:00.000','par01',2), 
('2015-08-01 11:06:00.000','par01',3), 
('2015-08-01 12:06:00.000','par01',3), 


('2015-08-01 11:03:00.000','par02',2), 
('2015-08-01 12:03:00.000','par02',2), 
('2015-07-01 11:06:00.000','par02',3), 
('2015-07-01 12:06:00.000','par02',3), 

('2015-09-01 11:03:00.000','par03',2), 
('2015-09-01 12:03:00.000','par03',2), 
('2015-08-01 11:06:00.000','par03',3), 
('2015-08-01 12:06:00.000','par03',3) 
+1

張貼這使得沒有任何意義。你想做什麼?視圖中不能有循環或參數。這將是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –

+0

我在這個問題上做了一些改變。你明白我的問題嗎? – JosepB

+0

不可以。你的問題根本沒有任何意義。結果的邏輯是什麼?數據來自哪裏? http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx –

回答

2

編輯:這是一個新的解決方案。在開始時,它將計算每個參數及其開始和結束的最後一個月的邊界。如果這很快,將主要取決於「參數」中不同值的計數。

而且:你需要索引!

SET LANGUAGE english; 

DECLARE @t table(timestamp datetime, parameter char(5), value int) 

INSERT @t values 
('2015-10-01 11:00:00.000','par01',1), 
('2015-10-01 12:00:00.000','par01',1), 
('2015-09-01 11:03:00.000','par01',2), 
('2015-09-01 12:03:00.000','par01',2), 
('2015-08-01 11:06:00.000','par01',3), 
('2015-08-01 12:06:00.000','par01',3), 

('2015-09-01 11:00:00.000','par02',1), 
('2015-09-01 12:00:00.000','par02',1), 
('2015-09-01 11:03:00.000','par02',2), 
('2015-09-01 12:03:00.000','par02',2), 
('2015-08-01 11:06:00.000','par02',3), 
('2015-08-01 12:06:00.000','par02',3), 

('2015-10-01 11:00:00.000','par03',1), 
('2015-10-01 12:00:00.000','par03',1), 
('2015-09-01 11:03:00.000','par03',2), 
('2015-09-01 12:03:00.000','par03',2), 
('2015-08-01 11:06:00.000','par03',3), 
('2015-08-01 12:06:00.000','par03',3); 

WITH MaxDates AS 
(
    SELECT MAX(x.timestamp) AS maxDate,x.parameter 
    FROM @t AS x 
    GROUP BY x.parameter 
) 
,MonthFirst AS 
(
    SELECT * 
      ,CONVERT(DATETIME, CAST(YEAR(maxDate) AS VARCHAR(4)) + CAST(REPLACE(STR(MONTH(maxDate),2),' ','0') AS VARCHAR(2)) + '01', 104) AS StartOfMonth 
    FROM MaxDates 
) 
,MonthBorders AS 
(
    SELECT * 
      ,DATEADD(SECOND,-1,DATEADD(MONTH,1,StartOfMonth)) AS EndOfMonth 
    FROM MonthFirst 
) 
SELECT * 
FROM @t AS t 
INNER JOIN MonthBorders AS mb ON t.parameter=mb.parameter AND t.timestamp BETWEEN mb.StartOfMonth AND mb.EndOfMonth 
ORDER BY t.parameter; 

一個增強可能是(保存一個DATEADD):

WITH MaxDates AS 
(
    SELECT MAX(x.timestamp) AS maxDate,x.parameter 
    FROM @t AS x 
    GROUP BY x.parameter 
) 
,MonthFirst AS 
(
    SELECT * 
      ,CONVERT(DATETIME, CAST(YEAR(maxDate) AS VARCHAR(4)) + CAST(REPLACE(STR(MONTH(maxDate),2),' ','0') AS VARCHAR(2)) + '01', 104) AS StartOfMonth 
    FROM MaxDates 
) 
,MonthBorders AS 
(
    SELECT * 
      ,DATEADD(MONTH,1,StartOfMonth) AS EndOfMonth 
    FROM MonthFirst 
) 
SELECT * 
FROM @t AS t 
INNER JOIN MonthBorders AS mb ON t.parameter=mb.parameter AND t.timestamp >= mb.StartOfMonth AND t.timestamp < mb.EndOfMonth 
ORDER BY t.parameter; 
+0

好的,您剛剛發表了評論,上個月對他們來說都不一樣...我會編輯我的解決方案... – Shnugo

+0

嗨@JosephBacardit,我編輯了我的答案。請注意,我更改了示例的數據,以便「par02」在9月份具有值,而不是在10月份。希望這是你所需要的。讓我們看看它有多快... – Shnugo

+0

我會看看 – JosepB

相關問題