SQL中的性能问题

时间:2011-10-11 06:30:38

标签: sql-server-2005

嗨,朋友,我遇到了大麻烦。

我的查询大约需要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

任何帮助或建议??

1 个答案:

答案 0 :(得分: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, - 已添加<​​/ p>

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 反复。

祝你好运。