Gap-less sequence where multiple transactions with multiple tables are involved

前端 未结 4 549
一整个雨季
一整个雨季 2020-12-22 03:54

I have a requirement (by law) for a gap-less numbers on different tables. The IDs can have holes in them but not the sequences.

This is something I have to either so

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-22 04:23

    This problem is impossible to solve by principle because any transaction can rollback (bugs, timeouts, deadlocks, network errors, ...).

    You will have a serial contention point. Try to reduce contention as much as possible: Keep the transaction that is allocating numbers as small as possible. Also, allocate numbers as late as possible in the transaction because only once you allocate a number contention arises. if you're doing 1000ms of uncontended work, and then allocate a number (taking 10ms) you still have a degree of parallelism of 100 which is enough.

    So maybe you can insert all rows (of which you say there are many) with dummy sequence numbers, and only at the end of the transaction you quickly allocate all real sequence numbers and update the rows that are already written. This would work well if there are more inserts than updates, or the updates are quicker than the inserts (which they will be), or there is other processing or waiting interleaved between the inserts.

提交回复
热议问题