请问这个存储过程可以怎么优化?

lexchou 2007-06-18 07:57:40
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 url=@url)
begin
--不存在数据, 直接插入
insert into posts values(@url,GETDATE(),@title,@pageId,@body,@digest,@text,1)
end
else
begin
select @digest2=@digest from posts where url=@url
if(@digest2=@digest)
begin
--旧数据, 直接更新重复编号
update posts set datetime=getdate(),duplicated=duplicated+1 where url=@url
end
else
begin
--新数据,重置所有数据
update posts set body=@body, text=@text, digest=@digest, pageId=@pageId, title=@title, datetime=getdate(), duplicated=1 where url=@url
end
end
GO





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

感觉第一个not exist和后面的读digest的应该可以省略成一行, 但是不知道怎么写.
这个存储过程需要被频繁的调用, 请问还有什么可以优化的措施呢?
...全文
200 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
free_pop2k 2007-06-18
  • 打赏
  • 举报
回复
if not exists(select * from posts where url=@url) ---->
if not exists(select 1 from posts where url=@url)

@body text --->
@text text --->最好不要用text
LamarChen 2007-06-18
  • 打赏
  • 举报
回复
建议使用游标。。。。。
lexchou 2007-06-18
  • 打赏
  • 举报
回复
url是不会变动的, 更新其他列也会影响url列的索引么?

chuifengde 2007-06-18
  • 打赏
  • 举报
回复
似乎再优化也没必要,如果把url作成索引,因为存在平凡的更新,所以还要重整索引,一样占时间
lidongri 2007-06-18
  • 打赏
  • 举报
回复
优化嘛
呵呵,楼下来
lexchou 2007-06-18
  • 打赏
  • 举报
回复
请问一下, 如果这个存储过程被频繁调用, 有什么额外的优化措施呢?
chuifengde 2007-06-18
  • 打赏
  • 举报
回复
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 url=@url

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

ly_liuyang 2007-06-18
  • 打赏
  • 举报
回复
Up一个算数~~
lexchou 2007-06-18
  • 打赏
  • 举报
回复
@digest2=@digest
应该是
@digest2=digest

打错了, 不管它
lexchou 2007-06-18
  • 打赏
  • 举报
回复
这个存储过程干什么的我在注释以及帖子里面说清楚了啊
Leaveye 2007-06-18
  • 打赏
  • 举报
回复
干吗的?

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧