mysql - 用户项目评级,每个用户 1 个评级,可更新 - 表结构?
我想让我的用户能够对网站上的每个项目进行从 1 到 5 的评分。他们还必须能够返回并根据需要修改每个项目的评分。最后,该项目的页面将显示所有用户查看时的最新平均评分。
至于PHP,我想我可以处理。我正在努力解决的是设想一组逻辑表来处理上述问题?
我正在考虑您的标准:
ID | ITEM## | USERID | RATING
表格设计,但感觉返回并不断检查用户之前是否对给定项目进行了评分,并在每次查看该项目时返回该结果,这将导致一组相当慢的脚本。 ..尤其是作为项目&用户数量随着时间的推移而增长。
对于这种情况,有人有更好的表格布局想法吗?我们正在研究自发布以来的 1500 多个项目,并且将来会更定期地添加,因此每个项目一个表有点过多,不是吗?
谢谢你!
I'd like to give my users the ability to rate each item on the site from 1 to 5. They also must be able to go back and revise their rating for each item as they please. Lastly, the item's page will display the average-to-date of all user ratings when viewed.
As for the PHP, I think I can handle that. What I'm struggling with is envisioning a logical set of tables to handle the above?
I was thinking just your standard:
ID | ITEM## | USERID | RATING
table design but feel that going back and constantly checking to see if a user had rated a given item before, and returning that result every time they view that item, would make for a rather slow set of scripts... especially as the item & user counts grow over time.
Does anyone have a better table(s) layout idea for this kind of situation? We're looking at 1500+ items from launch and more regularly added in the future, so a table per item is a bit excessive isn't it?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最好的方法。这称为多对多关系。您有许多用户和许多项目。一个用户可以对多个项目进行评分,并且一个项目可以由多个用户进行评分。
不要在您建议的表中添加 ID 字段,而是将键放在 ITEM 和 USERID 上。这样用户只能对一个项目评价一次。如果您尝试对其进行两次评分,则会出现错误。还要对这两个字段建立索引以加快表的搜索速度。
That is the best way to do it. It's called a many to many relation. You have many users and many items. One user can rate many items and one item can be rated by many users.
Instead of having an ID field in the table you proposed, put the key on ITEM and USERID. That way a user can only rate an item once. You will get an error if you try to rate it twice. Also index on both of these fields to speed up the searching of the table.
我想说在 ITEM 和 USERID 上放置唯一索引并使用“REPLACE INTO”更新行应该可行。我强烈建议您不要为每个项目制作一个表格,以节省一些负担。
I'd say putting a unique index over ITEM and USERID and updating the rows with "REPLACE INTO " should work. I would strongly descourage you to make a table per item just to save a bit of load.