MongoDB 自增 ID [英] MongoDB auto increment ID

查看:80
本文介绍了MongoDB 自增 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongodDB中自动生成的ID大小为12 Bytes,大整数的大小为8 bytes.我在 4 台运行 Ubuntu Server 的机器上有一个 mongodb 集群,但我现在只是在测试.插入只能通过一台作为 nodejs 服务器的服务器完成,但更新和删除可以使用世界各地运行本机 c 应用程序的各种机器和 nodejs 服务器来完成.

The size of the auto generated ID in MongodDB is 12 Bytes and the size of a big integer is 8 bytes. I have a mongodb cluster on 4 machines running Ubuntu Server, but I am just testing things now. Inserts can be done only through one server which is a nodejs server, but updates and deletes can be done using various machines running a native c application all around the world and the nodejs server.

既然我可以完全控制插入,那么使用自动递增 id 不是更好吗?

Since I have full control over inserts, wouldn't it be better to use an auto increment id?

  • 在 1MB 内存中,您可以保存 87381 个 12 字节 ID 和 131027 个 8字节 ID.
  • 使用自动递增 ID 是否值得?有什么好处除了节省内存?
  • 在性能方面,比较 8 字节 id 不会比 12 字节快字节 id,如果我做索引,它不会减少大小吗?

我是怎么做的:

我有这个文件

{id:0 latestId:174845423}

我很少将它增加 1,我的大部分插入都是批量插入,因此 nodejs 服务器修改要插入到循环中的文档,给每个文档增加一个 id,在插入操作结束时,我添加 i 更新具有最后一个 id 值的最新 id.

i very rarely increment it by 1, most of my inserts are bulk inserts, so the nodejs server modifies the documents to be inserted in a loop giving each one an incremented id, at the end of the insert operation i add i update the lastest id with the last id value.

推荐答案

自动增量"在分布式系统中始终是一个问题,因为它会产生瓶颈:每个新增量都需要读取之前的数据.. 就像其他一些并行请求一样.

"Auto-increment" is always a problem when working with a distributed system, as it creates a bottle-neck: Each new increment needs to read the previous data .. like some other parallel requests.

一般来说,自动增量"限制了并行性,特别是在分布式系统上.

Generally speaking, "auto-increment" restricts parallelism, specially on distributed systems.

这篇关于MongoDB 自增 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆