doupingzhi9674 2012-06-04 10:18
浏览 39

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

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条回答 默认 最新

  • dsx58940 2012-06-04 15:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?