我建立了up_counter存储过程,客服端每30秒钟调用一次up_counter更新数据,通常每次更新的数据40条左右。
实现使用中发现up_counter存储过程挺耗sql server资源如何优化这个存储过程?counter表设置主键:(date,hh,gate)CREATE PROCEDURE [dbo].[up_counter]
   @date datetime,
   @hh  char(2),
   @gate char(1),
   @up int,
   @down int,
   @total int,
   @net int
AS
BEGIN
   if not exists(select * from counter where date=@date and hh=@hh and gateno=@gate)
     insert into counter values(@date,@hh,@gate,@up,@down,@total,@net)
   else
      update counter set up=@up,down=@down,total=@total,net=@net
                   where date=@date and hh=@hh and gateno=@gate
    
END

解决方案 »

  1.   

    我已经在counter表设置主键:(date,hh,gate),还需要对条件字段(date,hh,gate)上添加索引吗?
      

  2.   

    保持現狀吧
    加了索引對insert會產生影響
      

  3.   

    谢谢大家既然up_counter 这个存储过程没有进一步优化的可能,
    那有其他更高效的新表数据方法吗?
      

  4.   

    建议将where后面的最具有限制性的条件放在前面,规则是大值在前,小值在后。
      

  5.   

    加入
    SET NOCOUNT ON;不显示操作的记录数
      

  6.   

    这样试试。。
    update counter set up=@up,down=@down,total=@total,net=@net
    where date=@date and hh=@hh and gateno=@gate
    if @@rowcount = 0
    insert into counter values(@date,@hh,@gate,@up,@down,@total,@net)
      

  7.   


    delete counter 
    where date=@date and hh=@hh and gateno=@gate
    insert into counter values(@date,@hh,@gate,@up,@down,@total,@net)