sql Server 大量数据批量插入 并发 死锁 情况怎么解决啊 这个存储过程能否优化

yonglaixiazaide 2011-03-23 04:40:43
以下 是存储过程,
其中 Tweeter,Tweets,TweetsCount都存在大量数据,最低千万级的
加了 nolock不行,大家还有什么好的解决办法啊


ALTER PROC [dbo].[TweetsAddOrUpdate]
@WebSiteId int,
@TweeterId bigint,
@WebTweetsId nvarchar(50),
@SourceId bigint,
@WebSourceId nvarchar(50),
@Content nvarchar(4000),
@CutWordStatus bit,
@OriginalText nvarchar(4000),
@ForwardCount int,
@CommentCount int,
@TweeterPostType nvarchar(100),
@TweeterPostTime datetime,
@TweetsType int,
@Location nvarchar(50),
@UpdateTime datetime,
@TweetsImportance int,
@ProvinceName nvarchar(50),
@CityName nvarchar(50)
as

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
declare @ProvinceId int
declare @cityId int
select @ProvinceId=CityId from City(nolock) where Name=@ProvinceName
select @cityId=CityId from City(nolock) where Name=@CityName and ParentId=@ProvinceId

DECLARE @TweetsId bigint

IF not exists (select TweetsId from Tweets(nolock) where WebTweetsId=@WebTweetsId and WebSiteId=@WebSiteId)
begin
INSERT INTO [Tweets]
([WebSiteId],[TweeterId],[WebTweetsId],[SourceId]
,[WebSourceId],[Content],[OriginalText]
,[ForwardCount],[CommentCount],[TweeterPostType]
,[TweeterPostTime],[TweetsType],[Location]
,[UpdateTime],TweetsImportance,ProvinceId,CityId)
VALUES
(@WebSiteId,@TweeterId,@WebTweetsId,@SourceId
,@WebSourceId,@Content,@OriginalText
,@ForwardCount,@CommentCount,@TweeterPostType
,@TweeterPostTime,@TweetsType,@Location
,getDate(),@TweetsImportance,@ProvinceId,@cityId)
SET @TweetsId=@@identity
end
else
begin
select @TweetsId=TweetsId from Tweets(nolock) where WebTweetsId=@WebTweetsId and WebSiteId=@WebSiteId
update Tweets set ForwardCount=@ForwardCount,CommentCount=@CommentCount,
UpdateTime=getDate(),TweetsImportance=@TweetsImportance where TweetsId=@TweetsId
end

update Tweets set SourceId=@TweetsId where WebSourceId=@WebTweetsId

if exists (select TweetsId from TweetsCounts(nolock)
where TweetsId=@TweetsId and CONVERT(nvarchar(10),UpdateTime,112)
=cONVERT(nvarchar(10),GETDATE(),112)
)
begin
update TweetsCounts set ForwardCount=@ForwardCount,CommentCount=@CommentCount where
CONVERT(nvarchar(10),UpdateTime,112)
=cONVERT(nvarchar(10),GETDATE(),112)
and TweetsId=@TweetsId
end
else
begin
INSERT INTO [TweetsCounts]
([TweetsId]
,[ForwardCount]
,[CommentCount]
,[UpdateTime])
VALUES
(@TweetsId,@ForwardCount,@CommentCount,GETDATE())
end

select @TweetsId
COMMIT TRANSACTION
...全文
661 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sfxdawn 2011-03-23
  • 打赏
  • 举报
回复
sqlbuckcopy自帶事務性功能,專門為大量數據處理的class
yonglaixiazaide 2011-03-23
  • 打赏
  • 举报
回复
貌似是我这里面的一个Update语句的问题,去掉就没这个问题了
Johnyin 2011-03-23
  • 打赏
  • 举报
回复
小批量处理,化整为零。。。
在一个事务中忌讳操作大量的数据。 循环用小批量处理一批,提交一批。
xiaotubie 2011-03-23
  • 打赏
  • 举报
回复
用bulkcopy,应该会好些
yonglaixiazaide 2011-03-23
  • 打赏
  • 举报
回复
高手帮帮忙啊

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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