在我的情况下,大多数ID将在大量移动客户端中生成,而不是在有限的一组服务器中生成.我想知道在这种情况下,是否存在合理的担忧.
对我来说,这听起来很糟糕.您使用的是两层体系 struct 吗?为什么移动客户端可以直接访问数据库?你真的想依靠网络安全吗?
总之,关于碰撞概率的一些讨论:
UUID和ObjectId都不依赖于它们的绝对大小,也就是说,它们都不是随机数,但它们遵循的方案试图系统地降低碰撞概率.如果是ObjectIds, their structure is:
- unix时代以来的4字节秒
- 3字节机器id
- 2字节进程id
- 3字节计数器
这意味着,与uuid相反,objectid是单调的(除了在一秒钟内),这可能是它们最重要的属性.单调索引将使B树的填充效率更高,它允许按id分页,并允许按id进行"默认排序"以使游标稳定,当然,它们带有易于提取的时间戳.这些是你应该注意的优化,它们可能是巨大的.
As you can see from the structure of the other 3 components, collisions become very likely if you're doing > 1k inserts/s on a single process (not really possible, not even from a server), or if the number of machines grows past about 10 (see birthday problem), or if the number of processes on a single machine grows too large (then again, those aren't random numbers, but they are truly unique on a machine, but they must be shortened to two bytes).
当然,要发生冲突,它们必须在这些方面匹配all个,因此即使两台机器具有相同的机器哈希,仍然需要客户机在完全相同的秒内插入相同的计数器值和相同的进程id,但是是的,这些值可能会冲突.