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.

    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站