数据库设计-好友活动
目前我正在设计一个小型的 twitter/facebook 类型的系统,用户应该能够看到他的朋友的最新活动。
我正在使用 ASP.NET 和 MySQL 数据库。
我的好友关系表如下:
|友谊id|朋友1|朋友2|已确认|
上表中的 Friend1 和 Friend2 是用户 ID。
用户活动表设计如下:
|activityId|用户 ID|活动|日期|
现在,我正在寻找查询用户最新 50 条好友活动的最佳方法。
例如,假设 Tom 登录系统,他应该能够看到他所有朋友的最新 50 条活动。
任何关于最佳实践的指示、查询或任何信息都值得赞赏。
Currently I am designing a small twitter/facebook kind of system, where in a user should be able to see his friends latest activities.
I am using ASP.NET with MySQL database.
My Friendships table is as follows:
|Friendshipid|friend1|Friend2|confirmed|
Friend1 and Friend2 in the above table are userids.
User activities table design following:
|activityId|userid|activity|Dated|
Now, I am looking for best way to query the latest 50 friend activities for a user.
For example, let's say if Tom logs into the system, he should be able to see latest 50 activities among all his friends.
Any pointers on the best practices, a query or any information is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很大程度上取决于 Friendships 表中存储的数据。例如,Friend1 和 Friend2 字段的存储顺序是什么?如果对于字段 (friend1,friend2),元组 (1, 2) 存在,那么 (2, 1) 也存在吗?
如果情况并非如此,那么这应该有效:
It largely depends on what data is stored in the Friendships table. For example, what order are the Friend1 and Friend2 fields stored in? If, for the fields (friend1, friend2) the tuple (1, 2) exists, will (2, 1) exist also?
If this is not the case, then this should work:
如果您有数据库性能问题,您可以重新定义Friendship表,如下所示:
当您查询最新50个活动时,SQL将是:
如果Friendships(userid)列上有索引,则它将使数据库有机会优化查询。
重新定义的友谊表需要在友谊发生时创建两个元组,但它仍然遵循业务规则,并且在需要时具有性能优势。
If you have database performance concern, you may redefine the friendship table as following:
When you query the latest 50 activities, the SQL would be:
And if there is a index on Friendships(userid) column, it would give the database the chance to optimize the query.
The friendship table redefined needs to create two tuples when a friendship occur, but it still obey the rule of business, and, has performance benefit when you need it.