码迷,mamicode.com
首页 > 数据库 > 详细

MSSQL 记一次删除数据特别慢

时间:2018-06-14 01:03:56      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:min   gen   student   weight   pre   generate   arch   rate   where   

x

["Table闪亮登场"]

主表:Base(BaseID Int,BaseName Nvarchar(200))

关系表:BaseForUser(BaseForUserID Int,BaseID Int)

["一些描述“]

要删除BaseID为1数据,当然BaseForUser中BaseID为1的数据也要删除...
(并且要删除的数据还不多,Base表一条数据,BaseForUser表中10条数据左右)
But  BaseForUser中数据比较多(客户比较多,你懂得),一亿多...

["第一个版本"]

Declare @BaseID Int = 1
Delete From BaseForUser Where BaseID = @BaseID

Delete From Base Where BaseID = @BaseID

--耗时 1分钟多...Pass

 

["第2个版本"]

第二个版本:
Delete Base From (Select BaseID From BaseForUser Where BaseID = @BaseID) U Where Base.BaseID = U.BaseID

--耗时50s...Pass

 

["第③个版本"]

--确定范围
Declare @User Table(BaseForUserID Int Primary Key)
Insert Into @User 
Select T.ClassTableGenerateForStudentID 
From BaseForUser U With(NoLock)
Where U.BaseID = @BaseID

Delete From BaseForUser
Where BaseForUserID Between ( Select Min(BaseForUserID) From @User ) 
And ( Select Max(BaseForUserID) From @User ) And BaseID = @BaseID

--耗时10s,好吧,先这样吧...-_-||

 

x

MSSQL 记一次删除数据特别慢

标签:min   gen   student   weight   pre   generate   arch   rate   where   

原文地址:https://www.cnblogs.com/love-zf/p/9180682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!