Danger of using 'IF EXISTS… UPDATE .. ELSE .. INSERT' and what is the alternative?

后端 未结 4 1203
既然无缘
既然无缘 2021-01-17 03:42

This question is related with Occasionally Getting SqlException: Timeout expired. Actually, I am using IF EXISTS... UPDATE .. ELSE .. INSERT heavily in my app.

4条回答
  •  借酒劲吻你
    2021-01-17 04:05

    Use MERGE

    Your SQL fails because 2 concurrent overlapping and very close calls will both get "false" from the EXISTS before the INSERT happens. So they both try to INSERT, and of course one fails.

    This is explained more here: Select / Insert version of an Upsert: is there a design pattern for high concurrency? THis answer is old though and applies before MERGE was added

提交回复
热议问题