这个存储过程主要用来记录个人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 [email protected] and [email protected] and [email protected]
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 [email protected] and [email protected] and [email protected]
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 [email protected]
end
------解决方案--------------------
从语句上看。。貌似没什么好优化的。。
------解决方案--------------------
這樣試試呢?
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 [email protected] and [email protected] and [email protected]
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 [email protected] and [email protected] and [email protected]
and datediff(dd, dateadded, getdate()) = 0)
GO