2017-04-14 66 views
0

我的存儲過程的目標是當我發送InstrumentID到存儲過程我想獲得最新值ChannelDescription(通過datetime和該時間的值)SQL Server - 循環遍歷表並獲取最新值和日期(性能問題)

我有3個表:Instruments < Channels < Datum

儀器

  • InstrumentID
  • INSTRUMENTNAME

頻道

  • 的channelID
  • InstrumentID
  • ChannelDescription
  • 量表

基準

  • DatumId
  • 渠道ID
  • DATATIME
  • NumericValue

下面的存儲過程的作品,但它是超慢:

ALTER PROCEDURE [dbo].[GetDataLastValue] 
    @InstrumentID INT 
AS 
BEGIN 
    SELECT 
     InstrumentId, 
     c.ChannelId, c.DataTime, 
     CONVERT(DECIMAL(38, 4), (NumericValue/c.Scale)) AS NumericValue, 
     c.Diag, ChannelDescription 
    FROM 
     dbo.Instrument 
    LEFT JOIN 
     (SELECT 
      ChannelId, 
      MAX(DateTime) as DataTime, 
      (SELECT TOP 1 NumericValue 
      FROM dbo.Datum 
      WHERE ChannelId = t.ChannelId 
      ORDER BY DateTime DESC) AS NumericValue, 
      (SELECT TOP 1 InstrumentId 
      FROM dbo.Channel 
      WHERE Channel.ChannelId = t.ChannelId) AS myinst, 
      (SELECT Scale 
      FROM dbo.Channel 
      WHERE Channel.ChannelId = t.ChannelId) AS Scale, 
      (SELECT Diag 
      FROM dbo.Channel 
      WHERE Channel.ChannelId = t.ChannelId) AS Diag, 
      (SELECT ChannelDescription 
      FROM dbo.Channel 
      WHERE Channel.ChannelId = t.ChannelId) AS ChannelDescription 
     FROM 
      dbo.Datum as t 
     GROUP BY ChannelId) c ON c.myinst = Instrument.InstrumentId 
    WHERE 
     InstrumentId = @InstrumentID AND c.Diag = 0 
    ORDER BY 
     ChannelDescription 
END 
+0

你的SP將無法正常工作時的3個查詢返回一個大於1分的結果:'(SELECT規模,由dbo.Channel WHERE Channel.ChannelId = t.ChannelId)','(SELECT DIAG FROM dbo.Channel WHERE Channel.ChannelId = t.ChannelId)','(SELECT ChannelDescription FROM dbo.Channel WHERE Channel.ChannelId = t.ChannelId)' – TriV

+0

@ TriV另一方面,ChannelId很可能是Channel表的主鍵,所以這不會發生。 –

回答

0

下面的代碼可能是f翠菊,但假設爲某一特定通道的DATATIME是截然不同的:

SELECT 
    InstrumentId, c.ChannelId, d.DataTime, 
    CONVERT(DECIMAL(38, 4), (NumericValue/c.Scale)) AS NumericValue, 
    c.Diag, ChannelDescription 
FROM 
    (SELECT 
     channelid, MAX(datatime) datatime 
    FROM 
     datum 
    GROUP BY 
     channelid) latest 
JOIN 
    channel c ON c.channelid = latest.channelid 
JOIN 
    datum d ON d.channelid = latest.channelid AND d.datatime = latest.datatime 
+0

不幸的是,這是行不通的。針對特定數據的日期時間是不同的。它的頻道沒有日期時間。 – Beachmat

+0

錯誤跟在消息207,級別16,狀態1,行7 無效的列名'datatime'。 消息207,級別16,狀態1,行15 無效的列名'datatime'。 消息207,級別16,狀態1,行2 無效的列名稱'DataTime'。 – Beachmat

+0

對不起,我發現了錯誤。謝謝,我仍在測試。 – Beachmat

0
Select 
    chl.InstrumentId, chl.ChannelId, 
    tbl.maxdate, 
    CONVERT(DECIMAL(38,4),(dt1.NumericValue/chl.Scale)) as NumericValue, 
    chl.ChannelDescription 
from 
    channelid chl 
inner join 
    (select 
     instrumentid, channelid, max(datatime) as maxdate 
    from 
     channel ch 
    inner join 
     datum dt on dt.channel id = ch.channelid 
    group by 
     instrumentid, channelid) tbl on tbl.instrumentid = chl.instrumentid 
             and chl.channelid = tbl.channelid 
inner join 
    datum dt1 on dt1.datatime = tbl.maxdate and dt1.channelid = tbl.channelid 
where 
    chl.instrumentid = @instrumentid 
+0

感謝您的幫助BOT這不工作不正確了Syntex附近的「DT」 – Beachmat

+0

更正語法錯誤,現在可以嘗試 – Kapil