Dml operation optimization in sql server

Some observations:

CASE WHEN coalesce(ltrim(rtrim(stag.col35)), '') = ''

is awkward and not SARGable. why not simply:

CASE WHEN stag.col35 = '' or stag.col35 is null

for all those case statements. Note that trimming leading and trailing spaces before comparing to an empty string has no effect. That is, SPACE(any length) = ''

See here for reference

Try to avoid expressions on the left side of comparisons that include columns in the row set, since they prevent SQL Server from using any indexes that may include those columns.

Speaking of columns, which ones are indexed? Hopefully those in ON clause of the MERGE statement at least. Preferably those in the WHERE clause as well

1 Like