javascript - MongoDB 效率最佳模式设计
问题描述
所以我对 mongoDB 还很陌生,但使用过 sql 相当多。我希望这是一个相当简单的问题,所以我会陷入困境。
在以下两种模式设计中,哪一种是最有效/最容易查询的?我将做相当简单的查询(我希望如此)。“销售”对象将比我在下面概述的要复杂得多。数据库中将有大约 30 家商店,预计在几年内会增长到 60 家。
- 选项1
{
name: "shop1",
sales:
[
{
weekEnding: "5/1/2020",
sales: 156.50,
customers: 5
},
{
weekEnding: "12/1/2020",
sales: 256.50,
customers: 12
},
]
},
{
name: "shop2",
sales:
[
{
weekEnding: "5/1/2020",
sales: 456.50,
customers: 9
},
{
weekEnding: "12/1/2020",
sales: 446.50,
customers: 19
},
]
},
- 选项 2
{
name: "shop1",
weekEnding: "5/1/2020",
sales: 156.50,
customers: 5
},
{
name: "shop1",
weekEnding: "12/1/2020",
sales: 256.50,
customers: 12
},
{
name: "shop2",
weekEnding: "5/1/2020",
sales: 456.50,
customers: 9
},
{
name: "shop2",
weekEnding: "12/1/2020",
sales: 446.50,
customers: 19
},
解决方案
我不是 MongoDB 专家,但我认为使用选项 2 会更好。使用选项 1,您的数据库中只有大约 30 个文档,并且所有文档都可能变得非常大,我认为 MongoDB 将不得不在磁盘上重新排列它们当它们改变大小时,因为对象应该保存在连贯的内存空间中。
而且我认为选项 2 没有任何问题。它可以很好地按商店索引销售额,并且不会导致任何性能问题。
推荐阅读
- r - 在 R 中使用正则表达式从 PDF 中提取特定单词
- php - 如何更改wordpress url结构
- python - TensorFlow fit 给出 TypeError: Cannot clone object 错误
- ios - viewcontroller 的 view 的背景色是决定 view 的大小还是 view 的可点击区域?
- angular - 尝试将文件上传到 Firebase 存储时出错 [Object object]
- flutter - 一起使用flutter bloc库和websockets的设计建议
- google-apps-script - 如何在 Google Apps 脚本中创建一个包含一天中时间列的条形图
- automata - 正则表达式 b 的个数可以被 3 整除的所有字符串
- python - CUDA 错误:AWS EC2 实例上的设备端断言触发错误
- python - Python 字典中的最小值