评级系统数据库结构
我有两个实体组。餐厅和用户。用户可以对餐厅进行评分(1-5)。每个用户的评分应该是可检索的。
餐厅(id,名称,.....,total_number_of_votes,total_voting_points) 用户(id,名称......)
评级(id,restaurant_id,user_id, rating_value)
我是否需要存储平均值,以便不需要每次都计算它?哪个表是存储 avg_ rating、total_no_of_votes、total_voting_points 的最佳位置?
I have two entity groups. Restaurants and Users. Restaurants can be rated (1-5) by users. And rating fromeach user should be retrievable.
Resturant(id, name, ..... , total_number_of_votes, total_voting_points )
User (id, name ...... )
Rating (id, restaurant_id, user_id, rating_value)
Do i need to store the avg value so that it need not be calculated every time ? which table is the best place to store avg_rating, total_no_of_votes, total_voting_points ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果你将平均值存储在某个地方;它仅在您上次计算时才是准确的。 (即您有 5 条评论;然后将平均值存储在某处。您又获得 5 条新评论,然后您保存的平均值不正确)。
我的观点是,这种逻辑非常适合中间层。计算平均值不应该非常消耗资源,并且实际上不应该影响性能。
如果你确实想将其存储在数据库中;我可能会将它们存储在自己的表中,并通过触发器更新这些值。然而,这可能比在中间层计算更加消耗资源。
Well, if you store the average value somewhere; it will only be accurate as of the last time you calculated it. (i.e. you have 5 reviews; then store the averages somewhere. You get 5 more new reviews, and then your saved average is incorrect).
My opinion is that this sort of logic is perfectly suited to a middle-tier. Calculating an average shouldn't be very resource intensive, and really shouldn't impact performance.
If you really want to store it in the database; I would probably store them in their own table, and update those values via triggers. However, this could be even more resource intensive than calculating it in the middle-tier.
某些数据库(例如 PostGreSQL)允许您将数组存储为行的一部分。例如
,您可以将最后 5 个评级保留在与餐厅相同的行中。当你得到一个新的评分时,将剩下的旧评分打乱,并在最后添加新的评分,然后计算平均值。
Some database, for example, PostGreSQL, allow you to store an array as part of a row. e.g.
So you could, for example, keep the last 5 ratings in the same row as the restaurant. When you get a new rating, shuffle the old ratings left, and add the new rating at the end, then calculate the average.