一个有关between and更更新的奇怪问题

廖家远 2017-12-23 12:53:50
表article_cross,数据量6亿行左右,结构如下,其它表数据量很少,可以忽略,整个库大概100G左右大小,使用简单日志:
SEQUENCE int identity PK字段
manufacturerId int
SupplierId int
OENbr varchar(50)
PartsDataSupplierArticleNumber varchar(50)
canDel bit
dupCount int
minSEQ int 用于存储重复值里面最少的SEQUENCE值(manufacturerId,SupplierId,OENbr,PartsDataSupplierArticleNumber这4个值都相同即为重复)
对manufacturerId,SupplierId,OENbr,PartsDataSupplierArticleNumber这4个字段进行了非聚集索引

现在是想要把重复值删除,现执行以下更新语句:
declare @s datetime,@e datetime
set @s = getdate()
declare @current int,@end int
set @current = 100000000
set @end = 100010000
update a set
minSEQ=
(
select min(SEQUENCE) from article_cross where manufacturerId=a.manufacturerId and OENbr=a.OENbr and SupplierId=a.SupplierId and PartsDataSupplierArticleNumber=a.PartsDataSupplierArticleNumber
)
from article_cross a
where
a.SEQUENCE between @current and @end
set @e = getdate()
select datediff(ms,@s,@e)
这里耗时六十多秒,如果将between and 直接改成常数a.SEQUENCE between 100000000 and 100010000,则只需零点几秒,不知道sqlserver把时间花在哪里了。

没办法,数据量太大,直接用一条语句删除的话,一般运行几个小时后报错,以及日志巨大,磁盘空间不够等等问题,现d只能分步来进行操作,谁知又遇到这个奇怪的问题。


...全文
347 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 版主 2017-12-23
  • 打赏
  • 举报
回复
二月十六 版主 2017-12-23
  • 打赏
  • 举报
回复
蹭个分
廖家远 2017-12-23
  • 打赏
  • 举报
回复
哦,改为 select top 1 SEQUENCE from article_cross where manufacturerId=a.manufacturerId and OENbr=a.OENbr and SupplierId=a.SupplierId and PartsDataSupplierArticleNumber=a.PartsDataSupplierArticleNumber order by SEQUENCE asc 后就没问题了

34,587

社区成员

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

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