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 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。