SQL Server : MERGE performance

前端 未结 1 375
攒了一身酷
攒了一身酷 2021-01-14 12:53

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 SHA

相关标签:
1条回答
  • 2021-01-14 13:18

    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)?
    0 讨论(0)
提交回复
热议问题