PHP-MYSQL 评分评论

发布于 2024-12-09 11:38:38 字数 327 浏览 2 评论 0原文

我有一个问题,你可能会认为它很愚蠢,但无论如何,

在制作 php 评论时,我认为如果人们可以用 +1/-1 对评论进行评分,那就太酷了,所以我在数据库中做了一个表来存放评论(id, user_id,评论,时间)和另一个评级评论(id_rate,id_comment,id_user,时间)我使用id_user,所以他不能对同一条评论进行两次评级

,问题如下:如果一篇博客文章有喜欢100 条评论,每条评论有 200 票(+1 或 -1),页面加载速度会不会太慢,大量查询只是为了加载一些评论?

是按照 DB 的正常形式执行此操作的另一种方法吗?

感谢您抽出时间,我真的很感激。

i have a question probably you will consider it stupid but anyway

while making in php comments , i thought that would be cool if people can rate a comment with +1/-1 so i did one Table in the DB for the comments (id, user_id, comment, time) and another one for the rating comments (id_rate, id_comment, id_user, time) i use the id_user so he can not rate two times the same comment

well the problem is the following: if a blog post has like 100 comments and each has like 200 votes (+1 or -1) wouldn't be the page too slow to load, lots and lots of queries just to load a few comments?

is another way around to do this following the normal forms of DBs?

Thanks for your time, i really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2024-12-16 11:38:38

要获取评论及其评级,可以在单个 SQL 查询中完成。当您有大量评论和评分时,速度会很慢,但对于您提到的数字而言,速度不会很慢。

如果您想让速度更快,您可以在评论上创建一个名为“评级”或类似名称的属性,并每小时更新一次(或者您认为足够的频率)左右。它不会是准确的,但会近似评论评级。但在必要之前,我们在这里讨论的是数百万条评论和评分。

这通常是通过物化视图来完成的,但不幸的是 MySQL 中没有实现...您可以通过编写 php 脚本并通过 cronjob 运行它来使用上面描述的方法。

使用这样的查询(未经测试):

SELECT * 
FROM comments, ratings 
WHERE comments.id = ratings.id_comment;

假设您为投票添加了一个值(+1 或 -1),您可以将它们分组并在一个查询中计算总评论评级,如下所示(未经测试):

SELECT comments.id, comments.user_id, comments.comment, 
       comments_time, SUM(ratings.value) AS rating
FROM comments, ratings
WHERE comments.id = ratings.id_comment
GROUP BY comments.id;

您现在将拥有所有评论字段和计算出的评论评级为字段评级。如果您想对其进行速度测试,您可以尝试插入一百万个虚拟评论和一百万个评分,然后查看查询需要多长时间。

为了进一步加快速度,您可以在 comments.id_comment 上添加索引,如下所示:

CREATE INDEX speedthisthingup ON comments(id_comment);

这应该对执行时间有很大帮助:)

祝你好运!

To get the comments and their ratings can be done in a single SQL-query. It will be slow when you have A LOT of comments and ratings, but not for the numbers you mention.

If you want to make it faster, you can create an attribute on the comments named "rating" or something like that, and update it every hour (or how often you think will suffice) or so. It will not be exact, but it will approximate the commentrating. But we are talking millions of comments and ratings here before it is nescesarry.

This would normally be done with a materialized view, but that is unfortunately not implemented in MySQL... You can use the method described above though by writing a php-script and running it through a cronjob.

Use a query like this (untested):

SELECT * 
FROM comments, ratings 
WHERE comments.id = ratings.id_comment;

assuming you add a value to your votes (+1 or -1) you can group them and calculate the total commentrating in one query like this (untested):

SELECT comments.id, comments.user_id, comments.comment, 
       comments_time, SUM(ratings.value) AS rating
FROM comments, ratings
WHERE comments.id = ratings.id_comment
GROUP BY comments.id;

You will now have all the commentfields and the calculated commentrating as the field rating. If you want to speedtest it you can try to insert a million dummy comments and a million ratings and see how long the query takes.

To speed it up even more you can add an index on comments.id_comment like this:

CREATE INDEX speedthisthingup ON comments(id_comment);

That should help the execution time a great deal :)

Good luck!

夜灵血窟げ 2024-12-16 11:38:38

您可以使用另一个名为 commentvote 的字段将当前评论投票缓存在 comments 表中。否则,使用适当的索引检查评级评论表以验证用户尚未投票应该相对较快。

You could cache the current comment vote in the comments table with another field called commentvote. Otherwise, checking the rating comments table to verify the user hasn't already voted should be relatively fast with appropriate indexes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文