简体   繁体   English

集合设置 MongoDB 缩放

[英]Collection set up MongoDB scaling

I'm trying to conceptualise the best way to scale a MongoDB Database.我正在尝试概念化扩展 MongoDB 数据库的最佳方法。

For example, say you want to create a site selling houses in different states using a MongoDB on the backend.例如,假设您想在后端使用 MongoDB 创建一个在不同州销售房屋的网站。 For future scalability and site performance, would it be best practice to have multiple collections for every state or list everything under USA and then search or find the state within the collection?对于未来的可扩展性和站点性能,最佳实践是为每个 state 设置多个 collections 还是列出 USA 下的所有内容,然后在集合中搜索或找到 state?

**Collection - Texas**

{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Modern loft","state":"Texas"}
{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Vacant Plot","state":"Texas"}
x 1000 entries

**Collection - USA**

{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Family Home","state":"Texas"}
{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Beach House","state":"California"}
x 1000 entries

It all depends on how would you search and present the data...这完全取决于您将如何搜索和呈现数据......

From performance perspective even it will be best to split the data in database per state and collections to represent the city, but this mean additional logic in your app to support the model and the logic to fetch some global details from all states/cities will become abit more complex.从性能的角度来看,即使最好将数据库中的数据按 state 和 collections 拆分来代表城市,但这意味着您的应用程序中有额外的逻辑来支持 model/cities 和全局详细信息将从中获取有点复杂。

If you have everything in single collection and you choose good indexes there will be no issues, at some point in time when the database grow you will just need to split the collection in more shards, but the logic to deal with the database will stay simple since you will have everything in single place.如果您将所有内容都放在单个集合中并且选择好的索引不会有任何问题,在数据库增长的某个时间点,您只需将集合拆分为更多分片,但处理数据库的逻辑将保持简单因为您将把所有东西都放在一个地方。 ( this is where your database of choice-mongodb can be of help since the sharding mechanism is build in and very easy to configure ) (这是您选择的数据库可以提供帮助的地方,因为分片机制是内置的并且非常易于配置)

So to me seems splitting the data to states as good compromise since there is no need the people from Oregon to query all the USA data to find few houses in some small village in Oregon when they can search only the collection in Oregon, but at the same time if you shard the collection and you have good sharding index that include the state, the query will go just to the shard where Oregon data is located... , with the sharding there is other trick for example if you search houses with swimming pool, but you dont include the state in the filter ( if it is sharding key ) the query will do scatter-gather to all shards to find those hauses...所以对我来说,将数据拆分到各州似乎是一个很好的折衷方案,因为俄勒冈州的人们不需要查询所有美国数据来在俄勒冈州的某个小村庄找到很少的房子,因为他们只能搜索俄勒冈州的集合,但在同时,如果您对集合进行分片并且您有良好的分片索引,其中包括 state,则查询将 go 仅针对俄勒冈州数据所在的分片...,使用分片还有其他技巧,例如,如果您搜索带有游泳的房屋池,但您没有在过滤器中包含 state (如果它是分片键),查询将对所有分片进行分散收集以找到那些豪斯......

I hope that answer your question...我希望能回答你的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM