针对我们的特定应用程序的MongoDB索引与阵列实现

这是问题所在。 我们正在使用MongoDB-PHP。</ p>

在我们的应用程序中,我们有许多用户可以发布帖子的用户组。 目前,我们正在以数组格式将这些组中的这些组维护在该组的文档中。 因此,当我们需要抓住前10个帖子时,我们可以使用切片操作从数组中获取它们。</ p>

例如:案例1 </ strong> </ p>

收集帖子://此收藏品存储各组的所有帖子</ p>

  {
{“_ id”:“1”,“post_text”:“ ....“,...}
{”_ id“:”2“,”post_text“:”....“,...}
}`
</ code> </ pre>

集合组://此集合包含每个组的文档</ p>

  {
{
“_id”:“1”
“name “:”组ABC“,
”post_ids“:{”1“,”2“....”100“}
//1,2..100表示​​该组相应帖子的MongoID
/ /所以当有人访问此页面时,我可以剪切该组的前10个帖子
}
}
</ code> </ pre>

`

相比之下 将这些帖子ID存储在群组文档中</ strong>,如果我们对群组ID使用索引</ strong>并将其存储在帖子集合中。

Eg:案例2 </ strong > </ p>

收集帖子</ p >

  {
{“_ id”:“1”,“group_id”:“1”,“post_text”:“....”,...}
{“ _id“:”2“,”group_id“:”2“,”post_text“:”....“,...}
}
</ code> </ pre>

另请注意,在案例1中,我们不必应用任何排序操作,因为数组元素按顺序推送,而在案例2中,我们将必须在查找操作之后应用排序(按时间戳标准),这将从内存中读取所有文档和 然后对它们进行排序。 </ p>

考虑到索引会存储在RAM中,谁的表现会更好? </ p>

如果此问题不明确,请与我们联系。</ p>
</ div>

展开原文

原文

Here is the issue. We are working with MongoDB-PHP.

In our application, we have many user groups where users can make posts. Presently we are maintaining the post ids these groups in the document of that group in array format. So that, when we need to grab first 10 posts we can grab them from the array using slice operation.

Eg: Case 1

collection posts: //this collection stores all the posts of various groups

{  
     {"_id":"1","post_text":"....",...}  
     {"_id":"2","post_text":"....",...}  
}  `    

collection groups: //this collection contains documents for each group

{  
    {  
        "_id":"1"  
        "name":"Group ABC",  
        "post_ids":{"1","2"...."100"}      
        //1,2..100 represents MongoIDs of corresponding posts of this group  
        //so i can slice first 10 posts of this group when someone visits this page
    }  
}

`
In contrast to storing these post ids in document of the group, if we use indexing on group id and store that in posts collection.
Eg: Case 2

collection posts

{  
    {"_id":"1","group_id":"1","post_text":"....",...}  
    {"_id":"2","group_id":"2","post_text":"....",...}  
} 

Also note that in Case 1 we do not have to apply any sorting operations as array elements are pushed in order while in Case 2 we will have to apply sort(by timestamp criteria) after the find operation, which would read all documents from memory and then apply sorting on them.

Whose performance would be better taking into consideration that indexes would be stored in RAM ?

Please let me know if the issue is not clear from this question.

1个回答



执行一个查询(案例#2)比执行两个查询要快。 此外,使文档更大(例如,将新帖子附加到#1中的post_id)是一个相当慢的操作。</ p>
</ div>

展开原文

原文

Doing one query (case #2) would be faster than doing two queries. Also, making documents bigger (e.g., appending new posts to post_ids in #1) is a fairly slow operation.

dtch60248
dtch60248 请看编辑过的问题
8 年多之前 回复
doubeiji2602
doubeiji2602 请提供你的答案。
8 年多之前 回复
doubi8512
doubi8512 thnx我回答后编辑了这个问题。 在案例2中,我们将应用sort,它将从内存中读取所有文档,然后对它们进行排序。
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐