Daily Shaarli

All links of one day in a single page.

Previous day

September 8, 2020

Next day

Please stop using this UPSERT anti-pattern - SQLPerformance.com
thumbnail

Locating the row to confirm it exists, only to have to locate it again in order to update it, is doing twice the work for nothing.

BEGIN TRANSACTION;

UPDATE dbo.t WITH (UPDLOCK, SERIALIZABLE) SET val = @val WHERE [key] = @key;

IF @@ROWCOUNT = 0
BEGIN
  INSERT dbo.t([key], val) VALUES(@key, @val);
END

COMMIT TRANSACTION;