简体   繁体   English

SQL 服务器更新表多连接无表锁

[英]SQL Server update table with multiple connections without tablelock

I've hit a bit of a problem with Microsoft SQL Server 12.0 .我在 Microsoft SQL Server 12.0遇到了一些问题。 My goal is to update multiple records at the same time without countering a tablelock (or any other lock).我的目标是同时更新多条记录,而不用对抗 tablelock(或任何其他锁)。
And this all just by using the JDBC driver, with the function resultSet.updateObject(<column>,<newValue>) and resultSet.updateRow() in a multithread situation with each thread an connection to the database.这一切只需在多线程情况下使用 JDBC 驱动程序和 function resultSet.updateObject(<column>,<newValue>)resultSet.updateRow()即可,每个线程都连接到数据库。

The situation that I try to achieve is like this:我试图达到的情况是这样的:

Table 'a' with 100.000 record
split into 5 connections who is dealing each 20.000 records
Each thread get its own connection with its own select query to update.(no records overlapping) 
Each thread is handling its own updates. Generating unique values depending op de application.
After the thread is completed it commits and closed the connection on that thread.

I know its possible with MySQL to use a query with pagination without any locking problems:我知道 MySQL 可以使用带有分页的查询而没有任何锁定问题:

select id, column_a, column_b from table a limit 0,20000
select id, column_a, column_b from table a limit 20000,20000
etc..

And with Oracle DB it can be done by filtering on the rowid使用 Oracle DB 可以通过过滤 rowid 来完成

select id, column_a, column_b from table a where rowid like '%1'
select id, column_a, column_b from table a where rowid like '%2'

Now I need to find the way to gain this on SQL Server I figured out that using the pagination of SQL Server creates a tablelock, like this query.现在我需要找到在 SQL 服务器上获得这个的方法我发现使用 SQL 服务器的分页会创建一个表锁,就像这个查询一样。

select id, column_a, column_b from a order by id offset 0 rows fetch first 20000 rows only

Even if I use the with(nolock) parameter within the query.即使我在查询中使用with(nolock)参数。 I also tried to change the locking level of the table to disabled.我还尝试将表的锁定级别更改为禁用。 And also tried to filter on the %%physloc%% just like doing it on oracle.并且还尝试像在 oracle 上一样过滤%%physloc%%

Can anyone hint me to a part that I am missing that will disable the tablelock?任何人都可以提示我缺少的部分会禁用桌锁吗? since each thread will not collide with a other session?因为每个线程都不会与其他 session 发生冲突?

(Updating with a single thread works, only this might take ages, thats why I want to split the table into separate workers. ( totaltime_on_singlethread / amount_threads = total_execution_time ) (使用单线程更新有效,只是这可能需要很长时间,这就是为什么我想将表拆分为单独的工作人员。( totaltime_on_singlethread / amount_threads = total_execution_time

Many thanks in advance for the help.非常感谢您的帮助。

I guess you have a deadlock on this table.我猜你在这张桌子上遇到了死锁。 T-sql provides this solution: SET TRANSACTION ISOLATION LEVEL READ COMMITTED; T-sql提供了这样的解决方案: SET TRANSACTION ISOLATION LEVEL READ COMMITTED; . . When it's run, every connection deals only with committed rows that are not being changed运行时,每个连接仅处理未更改的已提交行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM