首页 文章

MongoDB在一台机器上按日期分片

提问于
浏览
2

我们已经开始使用一个单一的mongodb但是我们没有一个集合增长到~300GB . 该集合包含具有日期字段的对象 . 但大多数情况下,我们只需要查询更近期的对象,然后查询历史性对象 . 所以我的问题是:是否可以通过日期字段在一台服务器上对此集合进行分片?更明确地说,我想将更新的对象分成一个节点,将旧对象分成另一个节点 . 而不是在n个分片上平均分配所有对象 .

是否有一个教程如何将现有的单个数据库(没有任何副本集)分成一个分片集群?

2 回答

  • 1

    从技术上讲,您不需要对内容进行分片,只需要为您的字段编制索引即可 . 是的,您可以在日期字段上创建索引,并且可以通过访问查询计划来查看该索引 db.collection.explain("executionStats")

    但是,选择分片键非常重要 . 选择分片键时要考虑的事情很少

    - Write scaling (high cardinality, Randomization)
    - Query Isolation. (read)
    

    选择日期字段实际上给出了非常高的基数,但是它无法进行随机化,因此所有文档都存储在单个分片中,因此它限制了系统的写入容量 . 出于同样的原因,不鼓励使用ObjectId作为分片键 .

    http://docs.mongodb.org/manual/core/sharding-shard-key/来自以上链接的内容.. "MongoDB generates ObjectId values upon document creation to produce a unique identifier for the object. However, the most significant bits of data in this value represent a time stamp, which means that they increment in a regular and predictable pattern. Even though this value has high cardinality, when using this, any date, or other monotonically increasing number as the shard key, all insert operations will be storing data into a single chunk, and therefore, a single shard. As a result, the write capacity of this shard will define the effective write capacity of the cluster."

  • 2

    根据您的描述,听起来您可能不需要分片,而是按日期将大型集合拆分为较小的集合 . 因此,实时收藏仅包含最新数据,旧数据会定期移动到其自己的存档集合中 . 假设您不一起查询新旧数据,这将起作用 .

相关问题