开发者

Is it a bad practice to use an identity column to determine the order of row creation? [duplicate]

开发者 https://www.devze.com 2023-03-11 18:24 出处:网络
This question already has answers here: 开发者_运维技巧Closed 11 years ago. Possible Duplicate: Can I use a SQL Server identity column to determine the inserted order of rows?
This question already has answers here: 开发者_运维技巧 Closed 11 years ago.

Possible Duplicate:

Can I use a SQL Server identity column to determine the inserted order of rows?

If an identity column is reseeded, then it can not be used be used to determine the order of row insertion, but I have no reason to ever reseed the identity.

Are there any reasons why I should not use the identity column to determine the order of creation?


Because it wouldn't be reliable would be the reason I would not use it. You might have two processes ask simultaneously for identity values and process 1 got the first value and process 2 got the second value but process 2 actually finished the transaction earlier and thus was inserted earlier. A datetime field for date inserted is the only reliable choice if you want to know the order that records were actually inserted.


It is not considered a good practice. For example, two processes doing inserts on a table in simultaneous transactions can in some servers have chunks of ids assigned to them, so any row inserted from one transaction will have a lesser id than any row inserted from the other transaction. Also, this can sometimes cause gaps in sequence of ids. And there may be also other scenarios something unexpected might happen.

In short, autoincremented ids are not always guaranteed to a be a continuous and ascending sequence.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号