Core component of SQL Server for storing, processing, and securing data
The real issue is usually:
- Merge acquires range locks
- serializable semantics protect against phantom inserts
- concurrent sessions in the same key ranges
- lock escalation may occur
- deadlocks/blocking become common
A good approach can be:
UPDATE testtable WITH (UPDLOCK)
SET
hjmpTS = @P0,
createdTS = @P1,
modifiedTS = @P2,
TypePkString = @P3,
OwnerPkString = @P4,
sealed = @P6,
uniqueid = @P7,
p_itemcode = @P8,
p_bucketuid = @P9,
aCLTS = @P10,
propTS = @P11
WHERE PK = @P5;
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO testtable
(
hjmpTS,
createdTS,
modifiedTS,
TypePkString,
OwnerPkString,
PK,
sealed,
uniqueid,
p_itemcode,
p_bucketuid,
aCLTS,
propTS
)
VALUES
(
@P0,
@P1,
@P2,
@P3,
@P4,
@P5,
@P6,
@P7,
@P8,
@P9,
@P10,
@P11
);
END
Advantages:
- Lower locking footprint
- better concurrency
- fewer deadlocks than merge