为每个用户创建一个集合与为所有用户创建一个集合


Create one collection for each user vs create one collection for all user

我在Ubuntu环境下使用PHP和MySQL作为社交网络系统。

我有名为 user_feed 的 MySQL 表,在此表中,我将提要保存为每个用户的feed_id我在 MySQL 中的表结构是:

    |user_feed_id | user_id | content_id | seen |

我有表user_follow,其中包含每个用户关注的数据,因此每个用户都有他/她关注的内容的记录集。

表结构:

follow_id | user_id  | content_id | 

user_feed表中我有超过 1.7 亿条记录,每个用户都有一组记录,user_follow表中我有超过 500 000 条记录。

我目前正在从MySQL迁移到MongoDB,所以我需要将此表转换为MongoDB中的集合。我认为要为user_feeduser_follow建立我的收藏,如下所示:

为每个用户创建集合,此集合有三个文档,一个用于关注 ID,另一个用于feed_ids,因此当我处理用户配置文件时,我将为每个成员运行一个集合的查询:

每个集合名称都引用user_id如下:

user_id_1 as collection name
            { user_id: '1'}
            {
                feed_ids: [
                 { content_id: '10', 'seen' : 1 },
                 { content_id: '11', 'seen' : 0 },
                 { content_id: '12', 'seen' : 1 },
                 { content_id: '13', 'seen' : 1 }
              ] 
            }
            {
             follow_ids: [
                 { content_id: '10' },
                 { content_id: '20'},
                 { content_id: '23'},
                 { content_id: '24'}
             ]
           }

user_id_2 as collection name
            { user_id: '2'}
            {
                feed_ids: [
                 { content_id: '14', 'seen' : 1 },
                 { content_id: '15', 'seen' : 0 },
                 { content_id: '16', 'seen' : 0 },
                 { content_id: '17', 'seen' : 0 }
              ] 
            }
            {
             follow_ids: [
                 { content_id: '22' },
                 { content_id: '23'},
                 { content_id: '24'},
                 { content_id: '25'}
             ]
           }

所以如果我有 70 000 个用户,那么我需要在 MongoDB 中创建 70 000 个集合

我还有另一种选择来创建它,例如:

一个集合的所有用户源,每个用户在集合中都有一个文档,如下所示:

{
        user_id: '1',
        feed_ids: [
            { content_id: '10'},
            { content_id: '11'},
            { content_id: '12'}
        ],
        follow_ids: [
            { content_id: '9'},
            { content_id: '11'},
            { content_id: '14'}
        ]
    }

并且这些表中的数据增长非常显着,我需要集合和文档能够执行所有操作,例如(插入,更新,选择,..)

我的feed_ids和follow_ids增长非常非常显着,我的查询是:

select content_id from user_feed where user_id =1 limit 10 offset 20;
update user_feed set seen = 1 where user_id =1
select count(content_id) from user_feed where seen = 0;
select content_id from user_follow where user_feed_id =1 limit 10 offset 20;
insert into user_feed (user_id,content_id,seen) values (1,23,0); 

第一个选项是我的用例的最佳解决方案还是第二个选项?

谢谢。

由于

nssize限制(2GB),每个用户的一个集合永远不会扩展,因为这意味着每个数据库限制为300万用户(假设该数据库仅包含用户...一旦你开始在多个数据库上跨越这样的事情,那么你就真的开始陷入实现问题。

此设置没有性能优势,因为主要好处是锁,而且是在数据库级别。我仍然认为我在上一段中的第一点会保留锁,即使它是按集合实现的。作为补充说明,由于MongoDB在更新未绑定的arays时处理单个文档的方式,您将得到低效的空间使用,这将产生"瑞士奶酪"效果并导致大量碎片进一步降低性能。

所以仅仅基于此,不,我不会为每个用户创建一个集合。