簡體   English   中英

SQL 服務器更新表多連接無表鎖

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

我在 Microsoft SQL Server 12.0遇到了一些問題。 我的目標是同時更新多條記錄,而不用對抗 tablelock(或任何其他鎖)。
這一切只需在多線程情況下使用 JDBC 驅動程序和 function 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 服務器上獲得這個的方法我發現使用 SQL 服務器的分頁會創建一個表鎖,就像這個查詢一樣。

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

即使我在查詢中使用with(nolock)參數。 我還嘗試將表的鎖定級別更改為禁用。 並且還嘗試像在 oracle 上一樣過濾%%physloc%%

任何人都可以提示我缺少的部分會禁用桌鎖嗎? 因為每個線程都不會與其他 session 發生沖突?

(使用單線程更新有效,只是這可能需要很長時間,這就是為什么我想將表拆分為單獨的工作人員。( totaltime_on_singlethread / amount_threads = total_execution_time

非常感謝您的幫助。

我猜你在這張桌子上遇到了死鎖。 T-sql提供了這樣的解決方案: SET TRANSACTION ISOLATION LEVEL READ COMMITTED; . 運行時,每個連接僅處理未更改的已提交行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM