您的位置:程序门 -> ms-sql server -> 应用实例



急!!!!存储过程高手请进,如何优化下面这个存储过程?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


急!!!!存储过程高手请进,如何优化下面这个存储过程?[已结贴,结贴人:vbvc]
发表于:2007-03-26 09:44:41 楼主
这个存储过程主要用来记录个人blog的访问情况的。

这个存储过程是否还可以优化?目前执行起来非常耗cpu,执行时间也很长,100万条记录左右,cpu就开始100%了。

请高手指点了!!!


create   proc   blog_insertvisiting

@visitor   nvarchar(50),
@ip   nvarchar(20),
@blogid   int

)
as


if   exists(select   top   1   vid   from   blog_visiting   with(nolock)
where   blogid=@blogid   and   visitor=@visitor   and   ip=@ip
and   convert(varchar(10),dateadded,121)=convert(varchar(10),getdate(),121))
begin

--如果不是匿名访客,更新访问时间
if   lower(@visitor)   != 'guest '
begin
    update   a   set   a.dateadded=getdate()   from   blog_visiting   a   with(rowlock)
    where   blogid=@blogid   and   visitor=@visitor   and   ip=@ip
and   convert(varchar(10),dateadded,121)=convert(varchar(10),getdate(),121)
end

end
else
begin

insert   blog_visiting(ip,visitor,   blogid,   dateadded)
values(@ip,@visitor,   @blogid,   getdate())

--更新访问量
update   blog_stats   with(rowlock)   set   visitcount=isnull(visitcount,0)+1   where   blogid=@blogid

end
发表于:2007-03-26 09:47:031楼 得分:5
从语句上看。。貌似没什么好优化的。。
发表于:2007-03-26 09:49:032楼 得分:0
但是执行起来怎么那么耗资源呢
跟踪了一下,这个是目前是数据库所有存储过程中最耗cpu的
发表于:2007-03-26 09:55:193楼 得分:0
请高手指点啊
发表于:2007-03-26 09:58:214楼 得分:45
這樣試試呢?

create   proc   blog_insertvisiting

@visitor   nvarchar(50),
@ip   nvarchar(20),
@blogid   int

)
as

update   a   set   a.dateadded=getdate()   from   blog_visiting   a   with(rowlock)
where   blogid=@blogid   and   visitor=@visitor   and   ip=@ip
and   datediff(dd,   dateadded,   getdate())   =0   and   lower(@visitor)   != 'guest '

insert   blog_visiting(ip,visitor,   blogid,   dateadded,   visitcount)
select   @ip,@visitor,   @blogid,   getdate(),   1
where   not     exists(select   top   1   vid   from   blog_visiting   with(nolock)
where   blogid=@blogid   and   visitor=@visitor   and   ip=@ip
and   datediff(dd,   dateadded,   getdate())   =   0)
go
发表于:2007-03-26 10:00:145楼 得分:0
另外,發現個問題.

如果表中存在數據的話,沒有更新訪問量,不存在數據的話,才更新訪問量.

好象一般都是不管是否存在數據都應該在訪問量上加1吧。

发表于:2007-03-26 10:04:096楼 得分:0
to:   paoluo(一天到晚游泳的鱼)

是这样的,我们统计的访问量,其实是每天的独立ip。
发表于:2007-03-26 10:13:497楼 得分:0
学习
发表于:2007-03-26 10:23:118楼 得分:0
请高手继续补充。。。。
发表于:2007-03-26 10:43:509楼 得分:0
?

我寫的那個測試了沒有?能用不?
发表于:2007-03-28 12:34:2110楼 得分:0
to:一天到晚游泳的鱼)  

刚出差了几天
还来不及测试
今天测试后告诉你
谢谢啊
发表于:2007-03-29 10:39:3611楼 得分:0
paoluo(一天到晚游泳的鱼)  

你的是可以的
但是性能没有什么特别的改进
而且业务逻辑跟我们的有所差异
发表于:2007-03-29 10:57:5612楼 得分:0
业务逻辑跟我们的有所差异?能指出哪裡嗎?

基本就是按照你上面語句的意思修改的。
发表于:2007-03-29 11:23:0613楼 得分:0
问题已经解决
谢谢   paoluo(一天到晚游泳的鱼)


快速检索

最新资讯
热门点击