SQL Server 2012: Running Multiple UPDATE FROM SELECTs in the Same Table Simultaneously

Situation:

1) There is a large TABLE1 (9 GB data, 20-bit idx space, 12M rows)
2) There are several UPDATE and UPDATE / SELECT on TABLE1 that are run one after another
3) Each UPDATE statement updates different columns
4) None of they are not used by the previously updated column for calculation into a new updated column
5) It takes some time to complete them.

Question:

I want to run these UPDATEs at the same time , but im interested in deadlocks. How to avoid them? ESTABLISH AN INSULATION LEVEL OF A NECESSARY INSTALLATION ?

UPDATES is as follows:

update TABLE1 set col1 = subs.col2
from (select ID, col2 from TABLE2) subs
where TABLE1.ID = subs.ID

update TABLE1 set col10 = col2+col3+col4

update TABLE1 set col100 = col2 + subs.col4
from (
    select 
        b.ID, a.col4
    from 
        TABLE3 a 
        join TABLE1 b on TABLE1.ID2 = TABLE3.ID2
) subs
where TABLE1.ID = subs.ID

update TABLE1 set col1000 = col2+col3+col4
from TABLE1
join TABLE4 on TABLE4.date = TABLE1.date
join TABLE5 on TABLE5.ID3 = TABLE1.ID 
+4
source share
3 answers

Dirty reads with READ UNCOMMITTED may work if the same columns are not updated and used in other sections, but I'm afraid this is a fragile solution.

For a more consistent solution, you can mix ROWLOCK / UPDLOCK / NOLOCK depending on the operations. Fe

UPDATE
    TABLE1 WITH (ROWLOCK)
SET
    col1 = TABLE2.col2
FROM
    TABLE1 WITH (ROWLOCK, UPDLOCK)
    INNER JOIN TABLE2 WITH (NOLOCK) ON (TABLE1.ID = TABLE2.ID)

If your statements update mostly different lines, then ROWLOCK can be omitted.

,

ALTER TABLE TABLE1 SET (LOCK_ESCALATION = DISABLE)

, ? , , , .

0

(1) . .

(2) TABLOCK vs TABLOCKX.

(3) ,

0

: , , , .

CREATE TABLE #tmp (
  RowID          int,
  NewCol1Value   ...,
  NewCol2Value   ...,
  NewCol2Value   ...
)

-- Insert into the tmp table
...

UPDATE Table1
  SET  Col1 = ISNULL(NewCol1Value, Col1),
       Col2 = ISNULL(NewCol2Value, Col2),
       ...
  FROM Table1 INNER JOIN #tmp ON Table1.RowID = #tmp.RowID
0

Source: https://habr.com/ru/post/1543189/


All Articles