评级系统查询
我在 MySQL 中构建了一个电影评级系统,但问题是,当我的查询汇总所有评级并进行除法时,它可能需要汇总数百万条记录。
我想到的一种解决方案是基本上将评级缓存在 memcached 中,并且只对不经常使用的项目进行评级操作。然而,即使如此,对于评级不高的电影,如果有人去检查评级,如果必须计算大量行,查询可能会花费很长时间。
我想到的另一个解决方案是建立一个不断更新表的临时表,但是如果电影评级很高并且有人试图访问它怎么办,我相信 INNODB 会进行行锁定,所以这会导致死锁或从长远来看?
I've built a rating system for movies in MySQL, however the concern is that when my query sums up all the ratings and divides it, it might have to sum up millions and millions of records.
One solution I thought of was to basically cache the rating in memcached and only do the rating operation for items which are not used a lot. However even then for movies which are not being rated a lot if someone does go check the rating that query might take a long time if it has to calculate a lot of rows.
Another solution i thought of was to build a temporary table which is constantly updating the table, however what if the movie is being rated a lot and someone is trying to access it, INNODB i believe does row locking, so will this cause a deadlock or something in the long run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于评级相当静态并且您的数据集很大,因此当用户对电影进行评级时,您可以缓存(非规范化)电影记录中的数据。您需要跟踪投票数和投票总和,以便平均值是准确的。您的电影评级会在您需要时动态计算。这是架构:
然后使用视图来帮助
假设您有一个如下所示的表:
您可以使用触发器来使投票总数保持最新:
如果可以更新投票,您将需要:
如果投票可以删除,你需要:
Since ratings are fairly static and your dataset is large, you could cache (denormalize) the data in the movie record when a user rates a movie. You need to keep track of the number of both votes and the sum of the votes, so the average is accurate. Your movie rating is dynamically calculated when you need it. Here's the schema:
then use a view to help out
assuming you have a table that looks like this:
you could use a trigger to keep the vote totals up to date for you:
if votes can be updated, you'll need:
if votes can be deleted, you'll need: