在设计哈希/索引系统时,时间戳是唯一 ID 的良好基础吗?

发布于 2024-11-02 04:17:37 字数 228 浏览 3 评论 0原文

我发现自己目前正在从事几个项目,其中包括数据库中需要唯一标识的用户条目。 (一些示例是用户脚本管理器我的应用程序列表。)

使用时间戳是管理这些条目并确保它们唯一的好方法或起点吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向地狱狂奔 2024-11-09 04:17:37

简而言之,不。

由于多种原因,时间戳并不是本质上唯一的,包括:

  • 多线程(包括多个主机上的线程);和
  • 时间戳粒度 - 即时钟本身 - 运行一对测试程序,一个发送消息,另一个用时间戳“处理”它们,你会看到你得到许多具有相同时间戳的条目,因为在许多平台上,系统时钟不会每毫秒发生变化。更糟糕的是,您不能依赖此行为:它取决于平台和速度。

因此,正如 colinmark 在评论中指出的那样,如果您需要唯一标识符,请使用 uuid。

In short, no.

Timestamps aren't inherently unique, due to various causes, including:

  • Multiple threading (including threads on multiple hosts); and
  • Timestamp granularity - that is, the clock itself - run up a pair of test programs, one which sends messages and one which "processes" them with their timestamp, and you'll see that you get many entries with the same timestamp, because on many platforms, the system clock doesn't change millisecond every millisecond. Worse, you can't depend on this behavior: it's dependent on platform and speed.

So, as colinmark noted in a comment, use a uuid if you need a unique identifier.

七禾 2024-11-09 04:17:37

我可能会连接时间戳和其他内容,例如 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.

烏雲後面有陽光 2024-11-09 04:17:37

不这样做有两个原因:

  • 不能保证时间戳是唯一的。与系统交互的两个用户完全有可能共享一个时间戳;即使这不太可能,但也是有可能的,因此您必须围绕它进行编码。
  • 您的唯一标识符应该毫无意义。时间戳有点毫无意义,但可能有人会要求您更新时间戳以处理时区问题,或夏令时,或“我打算昨天发布”;在这种情况下,您不能保证您会违反唯一性要求。

此外,MySQL 已经包含了一种分配唯一标识符(自动增量整数)的有保证的方法,并且 UUID 被广泛认为是实现这种跨平台的最佳方法。我会同意大多数人的意见......

There are two reasons not to do this:

  • there's no guarantee that timestamps are unique. It's entirely possible that two users interacting with the system will share a timestamp; even if this is unlikely, it's possible so you have to code around it.
  • your unique identifier should be meaningless. A timestamp is sorta kinda meaningless, but it's possible someone would ask you to update the timestamp to deal with timezone issues, or daylight savings, or "I meant to post that yesterday"; in that case, you can't guarantee you'd break the uniqueness requirement.

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文