在设计哈希/索引系统时,时间戳是唯一 ID 的良好基础吗?
I find myself currently working on several projects which include user entries in a database which need to be uniquely identified. (Some examples are a userscript manager and a list of my apps.)
Is using a timestamp a good way or starting point to manage these entries and ensure they are unique?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,不。
由于多种原因,时间戳并不是本质上唯一的,包括:
因此,正如 colinmark 在评论中指出的那样,如果您需要唯一标识符,请使用 uuid。
In short, no.
Timestamps aren't inherently unique, due to various causes, including:
So, as colinmark noted in a comment, use a uuid if you need a unique identifier.
我可能会连接时间戳和其他内容,例如 user_id 或条目的文本,如果可以的话,添加一些随机字符,然后对整个内容进行哈希处理。
像 SELECT MD5(CONCAT(entry_time, NOW())) 这样的东西可能就可以解决问题,而且它很好而且简单。
I would probably concatenate timestamp and something else, like a user_id or the text of the entry, and throw in some random characters if you can, then hash the whole thing.
Something like
SELECT MD5(CONCAT(entry_time, NOW()))
would probably do the trick, and it's nice and simple.不这样做有两个原因:
此外,MySQL 已经包含了一种分配唯一标识符(自动增量整数)的有保证的方法,并且 UUID 被广泛认为是实现这种跨平台的最佳方法。我会同意大多数人的意见......
There are two reasons not to do this:
Also, MySQL already includes a guaranteed way of assigning unique identifiers (auto increment integers), and UUIDs are widely considered to be the best way of doing this cross-platform. I'd go with the majority on this one...