2011-10-11 93 views
-1

嗨朋友我很麻煩。SQL中的性能問題

我有執行大約20分鐘的查詢(4.5分鐘的記錄)。

這是查詢。

SELECT 
    a.cmddefinitionid, 

    b.cmdinstanceid, 

    b.mobileid, 

    d.phonenumber, 

    d.hardwareid, 

    d.smsemail, 

    a.cmdid, 

    c.cmdname, 

    c.cmdxmldesc, 

    a.eventflag, 

    a.recurrenceflag, 

    a.paramflag, 

    a.filename, 

    a.paramname, 

    a.VALUE, 

    a.meterflag, 

    a.gosilentflag, 

    a.regurl, 

    b.scheduleddate, 

    -- e.TxnTypeID, -- Added 



    e.TxnID,-- Added 

    e.StatusMsg,-- Added 

    b.LastModified as TimeCreated,-- Added newly 

    d.PanelistID -- Added newly 

FROM 

(select CmdInstanceID, TxnTypeID, TxnID, StatusMsg 

       from ODM_TDCS.dbo.CMD_TXN 

       where TxnTypeID < 3 

       and TxnID IN (

       select max(TxnID) 

       from ODM_TDCS.dbo.CMD_TXN     

       group by CmdInstanceID) 

) AS e, 

    dbo.cmd_definition AS a, 
    dbo.cmd_instance AS b, 
    dbo.lu_cmd AS c, 
    dbo.lu_mobile AS d 
    WHERE 
    a.cmddefinitionid = b.cmddefinitionid 
    and 
    a.cmdid = c.cmdid and b.mobileid = d.mobileid and 
    b.cmdtypeid = 2 AND 
    b.scheduleddate > Getdate() - 2 AND 
    b.CmdInstanceID = e.CmdInstanceID 

現在出了這一點:

select CmdInstanceID, TxnTypeID, TxnID, StatusMsg 
      from ODM_TDCS.dbo.CMD_TXN 
      where TxnTypeID < 3 
      and TxnID IN (
      select max(TxnID) 
      from ODM_TDCS.dbo.CMD_TXN   
      group by CmdInstanceID) 

這上面走5分鐘,但如果我刪除此條件查詢獲取

0.17 sec執行。

任何幫助或建議?

+0

嗨,但我是一名Java開發人員,並且出現了一些問題,並要求我檢查我是否可以修復。我很少接觸到sql。這是現有的SQL其中i添加 (\t \t \t選擇CmdInstanceID,TxnTypeID,TxnID,StatusMsg \t \t \t從ODM_TDCS.dbo.CMD_TXN \t \t \t其中TxnTypeID <3 \t \t \t和TxnID IN( \t \t \t從ODM_TDCS.dbo.CMD_TXN \t \t \t \t \t \t選擇MAX(TxnID)\t \t \t group by CmdInstanceID condition。我也檢查了CMD_TXN表索引已經在那張表中。 – vibhas

回答

0

這種嘗試......

選擇 a.cmddefinitionid,

b.cmdinstanceid, 

b.mobileid, 

d.phonenumber, 

d.hardwareid, 

d.smsemail, 

a.cmdid, 

c.cmdname, 

c.cmdxmldesc, 

a.eventflag, 

a.recurrenceflag, 

a.paramflag, 

a.filename, 

a.paramname, 

a.VALUE, 

a.meterflag, 

a.gosilentflag, 

a.regurl, 

b.scheduleddate, 

- e.TxnTypeID - 新增

e.TxnID,-- Added 

e.StatusMsg,-- Added 

b.LastModified as TimeCreated,-- Added newly 

d.PanelistID -- Added newly 

FROM 

(Select * from (
select CmdInstanceID, TxnTypeID, TxnID, StatusMsg, 
ROW_NUMBER() over (partition by CmdInstanceID order by TxnID desc) as Row 
from ODM_TDCS.dbo.CMD_TXN 
where TxnTypeID < 3 
)as t where e.Row = 1 

) AS e, 

    dbo.cmd_definition AS a, 
    dbo.cmd_instance AS b, 
    dbo.lu_cmd AS c, 
    dbo.lu_mobile AS d 
    WHERE 
    a.cmddefinitionid = b.cmddefinitionid 
    and 
    a.cmdid = c.cmdid and b.mobileid = d.mobileid and 
    b.cmdtypeid = 2 AND 
    b.scheduleddate > Getdate() - 2 AND 
    b.CmdInstanceID = e.CmdInstanceID 

只用ROW_NUMBER函數,這將避免重複旅行ODM_TDCS.dbo.CMD_TXN 。

祝你好運。