Sunday, 8 September 2013

create one collection for each user vs create one collection for all user for building social network system

create one collection for each user vs create one collection for all user
for building social network system

I'm using PHP and MySQL for social network system under Ubuntu environment.
I have MySQL table named user_feed , in this table I save feeds as feed_id
for each user my table structure in MySQL is :
|user_feed_id | user_id | content_id | seen |
and I have table user_follow that contain data followed by every user , so
each user has set of record for what he/she followed content.
table structure:
follow_id | user_id | content_id |
in user_feed table I have more than 170 million record, and every user has
set of records and in user_follow table I have more than 500 000 record.
I currently work to migrate from MySQL to MongoDB. so I need to convert
this table to collection in MongoDB. I think to build my collection for
user_feed and user_follow as follow :
create collection for each user and this collection has three document one
for follow ids and another one for feed_ids , so when I handle user
profile I will run my query for one collection for each member:
each collection name is refer to user_id like :
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'}
]
}
so if I have 70 000 user then I need to create 70 000 collection in MongoDB
and I have another option to create it like :
all user feeds for one collection and each user has one document inside
collection like:
{
user_id: '1',
feed_ids: [
{ content_id: '10},
{ content_id: '11'},
{ content_id: '12'}
]
member_id: '2',
feeds: [
{ content_id: '9', 'activity_id' : 4 },
{ content_id: '11', 'activity_id' : 2 },
{ content_id: '14', 'activity_id' : 6 }
]
}
and the date in these tables grow very significantly and I need the
collections and documents to be able to do all operation like (insert,
update, select,..)
is the first option is the optimal solution for my use case or the second
one?
Thanks.

No comments:

Post a Comment