gpt4 book ai didi

performance - MongoDB 性能

转载 作者:可可西里 更新时间:2023-11-01 09:55:13 24 4
gpt4 key购买 nike

我需要为保存历史通知设计mongoDB 数据库。我考虑了两种可能的解决方案。

1)每个用户都会有 1 个这样的文档:

{
_id: 1234567890abcdefgh,
emailfrom: email1@example.com,
history: {
email2@example,com: {
{
read: 1,
text: 'hey man',
time: 2015-11-20-23-05-15
},
{
read: 0,
text: 'hey whats up',
time: 2015-11-20-23-10-10
}
},
email3@example,com: {
{
read: 1,
text: 'oooh',
time: 2015-11-20-23-05-11
}
}
}

2) 第二个解决方案是为每个通知自己的文档制作这样的:

{
_id: 1234567890abcd
emailfrom: email1@example.com,
emailto: email2@example.com,
text: 'hey man',
read: 1,
time: 2015-11-20-23-05-15
},
{
_id: 1234567890abcd
emailfrom: email1@example.com,
emailto: email2@example.com,
text: 'hey whats up',
read: 0,
time: 2015-11-20-23-05-15
},
{
_id: 1234567890abcd
emailfrom: email1@example.com,
emailto: email3@example.com,
text: 'oooh',
read: 1,
time: 2015-11-20-23-05-11
},

我的问题是关于这两种方法的性能。当有成千上万的通知时,我会想要选择、更新或查找:

1) 查找从 email1@example.com 发送的所有通知并读取:0 - 我认为第一种方法会更快

2) 我要保存新通知 - 我认为第二种方法会更快

3) 我想更新 read 0 -> read 1 emailfrom email1@example.com emailto email2@example.com- 我不知道哪种方法会更快。

谁能帮我解决这个问题?哪一种是保存此类数据的正确方法?感谢您的任何评论!

最佳答案

我猜第一种方法中存在拼写错误。 “email2@example,com”和“email3@example,com”的类型应该是数组吧?不管怎样,根据您提到的用例,我更愿意选择选项 2。只需向该表添加适当的索引,例如“emailfrom+read”的复合索引和"emailfrom+emailto"可以确保您在查询期间具有高性能。

最常见的情况是通过 emailfrom 进行查询,而另一种情况是选项 1 无济于事。而且由于将来会有很多通知,您仍然认为 mongoDB 中每个文档大小有 16M 的限制。

关于performance - MongoDB 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33837463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com