简体   繁体   中英

SQL Server update table with multiple connections without tablelock

I've hit a bit of a problem with Microsoft SQL Server 12.0 . My goal is to update multiple records at the same time without countering a tablelock (or any other lock).
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.

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:

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

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.

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. 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.

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?

(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 )

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; . When it's run, every connection deals only with committed rows that are not being changed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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