Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在SQL Server中忙于删除/更新的表中插入行的有效方法_Sql Server - Fatal编程技术网

Sql server 在SQL Server中忙于删除/更新的表中插入行的有效方法

Sql server 在SQL Server中忙于删除/更新的表中插入行的有效方法,sql-server,Sql Server,这里是我的问题:我有10000行要插入到表格中。此表总是忙于许多并发用户执行的大量删除和更新。我正在考虑将行分块插入,如下所述: Declare @i int = 1 while @i > 0 begin begin transaction insert top (1000) id, col1, col2, col3 into TableX select id, col1, col2, col3 from

这里是我的问题:我有10000行要插入到
表格中。此表总是忙于许多并发用户执行的大量删除和更新。我正在考虑将行分块插入,如下所述:

Declare @i int = 1

while @i > 0
begin
   begin transaction
       insert top (1000) id, col1, col2, col3 
       into TableX
           select id, col1, col2, col3 
           from TableY as a
           where not exists (select 1 from TableX as b where a.id = b.id)
       set @i = @@rowcount

   commit transaction
end
现在我能想到的一个问题是。如果我插入一条记录并在另一个连接中进行删除时提交它。如果我的记录处于delete语句的where条件,则插入的行有可能被删除。我想我不会成为其他用户阻塞的原因。我们使用的是读提交隔离级别

所以我的问题是我应该继续插入这些行,还是有什么我需要担心的

下面是TableX和TableY的DDL语句

create tableX
(id int primary key,
col1 varchar(100),
col2 varchar(100),
col3 varchar(100)
)
create nonclustered index ncli on TableX(cl1) include (col2,col3)

create tableY
(id int primary key,
col1 varchar(100),
col2 varchar(100),
col3 varchar(100)
)
create nonclustered index ncli on TableY(cl1) include (col2,col3)

谢谢…

请为您的表(带索引)提供
DDL
。@Devart:请查看我编辑的问题。
Recovery model=SIMPLE
?为什么不尝试在一条语句中插入10000行的简单方法呢,它不像数百万行……值得您花时间阅读SQL Server使用的并发模型:SQL Server默认使用悲观并发。尝试插入,如果通过监视发现问题或出现死锁,则需要找到解决该问题的方法,而不是先发制人地尝试解决可能存在或不存在的问题。请为表(带索引)提供
DDL
..@Devart:请查看我编辑的问题。
恢复模型=简单
?为什么不尝试在一条语句中插入10000行的简单方法,这不像数百万行……值得花时间阅读SQL Server使用的并发模型:默认情况下,SQL Server使用悲观并发。尝试插入,如果通过监视发现问题,或出现死锁,则需要找到解决该问题的方法,而不是先发制人地尝试解决可能存在或不存在的问题。