【问题标题】:SQL Server update table with multiple connections without tablelockSQL Server 更新具有多个连接的表而没有表锁
【发布时间】:2020-08-23 20:32:11
【问题描述】:

我在使用 Microsoft SQL Server 12.0 时遇到了一点问题。 我的目标是同时更新多条记录,而不用对抗 tablelock(或任何其他锁)。
这一切只需使用 JDBC 驱动程序,在多线程情况下使用函数 resultSet.updateObject(<column>,<newValue>)resultSet.updateRow(),每个线程都连接到数据库。

我试图达到的情况是这样的:

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.

我知道 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..

使用 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'

现在我需要找到在 SQL Server 上获得此功能的方法 我发现使用 SQL Server 的分页会创建一个表锁,就像这个查询一样。

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

即使我在查询中使用with(nolock) 参数。 我还尝试将表的锁定级别更改为禁用。 并且还尝试过滤 %%physloc%% 就像在 oracle 上一样。

任何人都可以提示我缺少的部分会禁用 tablelock 吗?因为每个线程都不会与其他会话发生冲突?

(使用单线程更新有效,只是这可能需要很长时间,这就是为什么我想将表拆分为单独的工作人员。(totaltime_on_singlethread / amount_threads = total_execution_time

非常感谢您的帮助。

【问题讨论】:

    标签: java sql sql-server database jdbc


    【解决方案1】:

    我猜你在这张桌子上遇到了死锁。 T-sql 提供了这个解决方案:SET TRANSACTION ISOLATION LEVEL READ COMMITTED;。运行时,每个连接仅处理未更改的已提交行

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2016-05-07
      • 1970-01-01
      • 2014-09-22
      • 2017-10-13
      • 2015-11-22
      相关资源
      最近更新 更多