Skip to content Skip to sidebar Skip to footer

Sql Server : Merge Performance

I have a database table with 5 million rows. The clustered index is auto-increment identity column. There PK is a code generated 256 byte VARCHAR which is a SHA256 hash of a URL, t

Solution 1:

Your UPDATE clause in the MERGE updates showCount. This requires a key lookup on the clustered index.

However, the clustered index is also declared non-unique. This gives information to the optimiser even though the underlying column is unique.

So, I'd make these changes

  • the clustered primary key to be autoIncID
  • the current PK on imageSHAID to be a standalone unique index (not constraint) and add an INCLUDE for showCount. Unique constraints can't have INCLUDEs

More observations:

  • you don't need nvarchar for the hash or URL columns. These are not unicode.
  • A hash is also fixed length so can be char(64) (for SHA2-512).
  • The length of a column defines how much memory to assign to the query. See this for more: is there an advantage to varchar(500) over varchar(8000)?

Post a Comment for "Sql Server : Merge Performance"