2016-06-21 288 views
1

首先我想說,我對SQL相當陌生,所以這可能看起來像一個愚蠢的問題。 因此,在此代碼中,我收到一個Date作爲參數,將61分鐘添加到它並檢查兩者之間的值。 然後我總結每列的值並將其存儲在另一個表中。 該代碼工作得很好,我想知道的是,如果有更好的方法來做到這一點,以及如何做到這一點(不使用這麼多的行或重複代碼)需要幫助優化SQL查詢

在此先感謝。

alter procedure Contagem 
@date datetime 

as 
begin 
    declare 
    @Sala1 float, 
    @Sala2 float, 
    @Sala3 float, 
    ... 
    @Sala26 float, 
    @Sala27 float, 
    @Sala28 float, 
    @dateplus datetime 

    set @Teste = 1 
    set @dateplus = (select DATEADD(MINUTE,61,@date)) 

    set @Sala1 = (select sum(Sala_1_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 
    set @Sala2 = (select sum(Sala_2_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 
    set @Sala3 = (select sum(Sala_3_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 
    ... 
    set @Sala26 = (select sum(Sala_26_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 
    set @Sala27 = (select sum(Sala_27_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 
    set @Sala28 = (select sum(Sala_28_Energia) from Energia_15min where Time_Stamp between @date and @dateplus) 

    Insert into Custos_hora values (@date,@Sala1,@Sala2,@Sala3,@Sala4,@Sala5,@Sala6,@Sala7,@Sala8,@Sala9,@Sala10,@Sala11,@Sala12,@Sala13,@Sala14,@Sala15,@Sala16,@Sala17,@Sala18,@Sala19,@Sala20,@Sala21,@Sala22,@Sala23,@Sala24,@Sala25,@Sala26,@Sala27,@Sala28) 
end 
+3

你真的需要閱讀規範化。你有重複的列,這使得事情變得非常困難。通過將所有變量設置在單個select語句中,而不是一次又一次地敲入同一個表,您當然可以使此更好。但最好的選擇是將這個表完全重構爲一個規範化的表結構。 –

+0

@Dumbrica:目前爲止,你所得到的最佳答案是Sean Lange在我看來的評論。 –

+0

另一個值得關注的問題是你在float上使用SUM。這表明您需要對這些值進行某種精度。 float數據類型是一個近似的數據類型,不能保存每個值。如果你想要的準確性,你應該考慮切換到十進制或數字。 –

回答

4

您可以按照您目前正在做的那樣一次而不是28次地打那張表。

INSERT INTO Custos_hora 
SELECT 
SUM(Sala_1_Energia) 
,SUM(Sala_2_Energia) 
,SUM(Sala_3_Energia) 
,SUM(Sala_4_Energia) 

FROM Energia_15min 

WHERE Time_Stamp between @date and @dateplus 

這被認爲是最好的做法,宣佈你插入到這樣的領域;

INSERT INTO Custos_hora (Field1, Field2, Field3, Field4) 
SELECT 
SUM(Sala_1_Energia) 
,SUM(Sala_2_Energia) 
,SUM(Sala_3_Energia) 
,SUM(Sala_4_Energia) 

FROM Energia_15min 

WHERE Time_Stamp between @date and @dateplus 

另外,什麼是變量@Teste?它似乎沒有在任何地方使用。而且你似乎也沒有宣佈@date

+0

忽略@teste,我之前用它來測試一些東西,並忘記刪除它。 – Dumbrica

+0

謝謝你的幫助 – Dumbrica

+0

是的,我確定我們都已經在那裏用測試代碼:)如果這些答案中的任何一個都有幫助,那麼請隨時註冊它們。此外,如果你可以標記爲接受的答案,那麼將不勝感激。 –

1

你可以做,而不是執行以下操作:

Alter Procedure Contagem (@Date DateTime) 
As Begin 
    Insert Custos_hora 
    Select @Date, 
      Sum(Sala_1_Energia), 
      Sum(Sala_2_Energia), 
      Sum(Sala_3_Energia), 
      Sum(Sala_4_Energia), 
      Sum(Sala_5_Energia), 
      Sum(Sala_6_Energia), 
      Sum(Sala_7_Energia), 
      Sum(Sala_8_Energia), 
      Sum(Sala_9_Energia), 
      Sum(Sala_10_Energia), 
      Sum(Sala_11_Energia), 
      Sum(Sala_12_Energia), 
      Sum(Sala_13_Energia), 
      Sum(Sala_14_Energia), 
      Sum(Sala_15_Energia), 
      Sum(Sala_16_Energia), 
      Sum(Sala_17_Energia), 
      Sum(Sala_18_Energia), 
      Sum(Sala_19_Energia), 
      Sum(Sala_20_Energia), 
      Sum(Sala_21_Energia), 
      Sum(Sala_22_Energia), 
      Sum(Sala_23_Energia), 
      Sum(Sala_24_Energia), 
      Sum(Sala_25_Energia), 
      Sum(Sala_26_Energia), 
      Sum(Sala_27_Energia), 
      Sum(Sala_28_Energia) 
    From Energia_15min 
    Where Time_Stamp Between @Date And DateAdd(Minute, 61, @Date) 
End 
1

多SELECT語句將進行多次表掃描,提高磁盤IO和內存利用率。它可以在單選語句中完成。

alter procedure Contagem 
     @date datetime 

     as 
     begin 


     declare 
     @Sala1 float, 
     @Sala2 float, 
     @Sala3 float, 
     @Sala4 float, 
     @Sala5 float, 
     @Sala6 float, 
     @Sala7 float, 
     @Sala8 float, 
     @Sala9 float, 
     @Sala10 float, 
     @Sala11 float, 
     @Sala12 float, 
     @Sala13 float, 
     @Sala14 float, 
     @Sala15 float, 
     @Sala16 float, 
     @Sala17 float, 
     @Sala18 float, 
     @Sala19 float, 
     @Sala20 float, 
     @Sala21 float, 
     @Sala22 float, 
     @Sala23 float, 
     @Sala24 float, 
     @Sala25 float, 
     @Sala26 float, 
     @Sala27 float, 
     @Sala28 float, 
     @dateplus datetime 

    set @Teste = 1 
    set @dateplus =DATEADD(MINUTE,61,@date)) 

    Insert into Custos_hora 
     select sum(Sala_1_Energia), 
     sum(Sala_2_Energia), 
     sum(Sala_3_Energia), 
     sum(Sala_4_Energia), 
     sum(Sala_5_Energia), 
     sum(Sala_6_Energia), 
     sum(Sala_7_Energia), 
     sum(Sala_8_Energia), 
     sum(Sala_9_Energia), 
     sum(Sala_10_Energia), 
     sum(Sala_11_Energia), 
     sum(Sala_12_Energia), 
     sum(Sala_13_Energia), 
     sum(Sala_14_Energia), 
     sum(Sala_15_Energia), 
     sum(Sala_16_Energia), 
     sum(Sala_17_Energia), 
     sum(Sala_18_Energia), 
     sum(Sala_19_Energia), 
     sum(Sala_20_Energia), 
     sum(Sala_21_Energia), 
     sum(Sala_22_Energia), 
     sum(Sala_23_Energia), 
     sum(Sala_24_Energia), 
     sum(Sala_25_Energia), 
     sum(Sala_26_Energia), 
     sum(Sala_27_Energia), 
     sum(Sala_28_Energia) from Energia_15min where Time_Stamp between @date and @dateplus 
     end