当前位置: 代码迷 >> Sql Server >> 请教这个存储过程可以如何优化
  详细解决方案

请教这个存储过程可以如何优化

热度:11   发布时间:2016-04-27 20:30:29.0
请问这个存储过程可以怎么优化?
IF   EXISTS   (SELECT   name  
      FROM       sysobjects  
      WHERE     name   =   N 'add_url '  
      AND       type   =   'P ')
        DROP   PROCEDURE   add_url
GO

CREATE   PROCEDURE   add_url  
@url   varchar(255),  
@title   nvarchar(255),
@body   text,
@text   text,
@digest   char(32),
@pageId   int

AS
declare   @digest2   char(32)
if   not   exists(select   *   from   posts   where   [email protected])
begin  
--不存在数据,   直接插入
insert   into   posts   values(@url,GETDATE(),@title,@pageId,@body,@digest,@text,1)
end
else
begin
select   @[email protected]   from   posts   where   [email protected]
if(@[email protected])
begin
--旧数据,   直接更新重复编号
update   posts   set   datetime=getdate(),duplicated=duplicated+1   where   [email protected]
end
else
begin
--新数据,重置所有数据
update   posts   set   [email protected],   [email protected],   [email protected],   [email protected],   [email protected],   datetime=getdate(),   duplicated=1   where   [email protected]
end
end
GO


意思就是说,   如果插入的数据不存在([email protected]=url),   则将数据插入进去.  
如果存在了,   则比较是不是新数据,  
如果不是新数据(@[email protected]),   则更新duplicated增1,    
如果是新数据,   则刷新所有列

感觉第一个not   exist和后面的读digest的应该可以省略成一行,   但是不知道怎么写.  
这个存储过程需要被频繁的调用,   请问还有什么可以优化的措施呢?

------解决方案--------------------
干吗的?
------解决方案--------------------
Up一个算数~~
------解决方案--------------------
CREATE PROCEDURE add_url
@url varchar(255),
@title nvarchar(255),
@body text,
@text text,
@digest char(32),
@pageId int

AS
declare @digest2 char(32)

select @digest2=digest from posts where [email protected]

if @digest2 is null
insert into posts values(@url,GETDATE(),@title,@pageId,@body,@digest,@text,1)
else if @[email protected]
update posts set datetime=getdate(),duplicated=duplicated+1 where [email protected]
else
update posts set [email protected], [email protected], [email protected], [email protected], [email protected], datetime=getdate(), duplicated=1 where [email protected]


------解决方案--------------------
优化嘛
呵呵,楼下来

------解决方案--------------------
似乎再优化也没必要,如果把url作成索引,因为存在平凡的更新,所以还要重整索引,一样占时间
------解决方案--------------------
建议使用游标。。。。。
  相关解决方案