Encountering 'Cannot Insert Duplicate Key in Object' Error Due to Parallel Query Execution

Nechba 20 Reputation points
2024-03-27T07:49:45.6833333+00:00

The issue at hand involves encountering a "Cannot insert duplicate key in object" error during parallel execution of queries by multiple threads. The error indicates that duplicate keys are being attempted to be inserted into the database object, despite the absence of duplicate data. This problem arises due to concurrent execution of the same query by multiple threads simultaneously. It is suspected that the parallelism inherent in this scenario is causing contention, leading to the duplication of keys. However, the proposed solution of setting MAXDop = 1 would adversely impact performance, which is undesirable. Therefore, an alternative solution is sought to resolve the issue without sacrificing performance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
{count} votes

1 answer

Sort by: Most helpful
  1. 博雄 胡 190 Reputation points
    2024-03-27T08:23:41.38+00:00

    It has nothing to do with concurrency.

    It is due to the occurrence of duplicate values in subsequent insertions triggered by the trigger, rather than the occurrence of duplicate values in the insertions to WorkTABLE_xxxxxx.