SQL COUNT(col) 与额外的日志记录列...效率?

发布于 2024-12-01 22:11:30 字数 903 浏览 0 评论 0原文

我似乎找不到太多关于这方面的信息。

我有一个表来记录用户评论。我有另一个表来记录其他用户对每条评论的喜欢/不喜欢。 因此,当选择要在网页上显示的数据时,存在需要连接和子查询来统计所有喜欢/不喜欢的复杂查询。 我的示例是一个查询,有人善意地帮助我在这里实现所需的结果:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=1)likes,
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=0)dislikes,
  comment_likers.liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers  ON ( comments.comment_id = comment_likers.comment_id 
  AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;

但是,如果我向 COMMENTS 表添加了喜欢和不喜欢的列,并创建了一个触发器来自动递增/递减这些列,因为喜欢被插入/删除/更新到 LIKER 表,那么 SELECT 语句将比现在更简单、更高效。我问,使用 COUNTS 进行这个复杂的查询还是使用额外的列和触发器更有效?

概括地说,在定期查询时,计数或有一个额外的列用于计数是否更有效?

I can't seem to find much information about this.

I have a table to log users comments. I have another table to log likes / dislikes from other users for each comment.
Therefore, when selecting this data to be displayed on a web page, there is a complex query requiring joins and subqueries to count all likes / dislikes.
My example is a query someone kindly helped me with on here to achieve the required results:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=1)likes,
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=0)dislikes,
  comment_likers.liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers  ON ( comments.comment_id = comment_likers.comment_id 
  AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;

However, if I added a likes and dislikes column to the COMMENTS table and created a trigger to automatically increment / decrement these columns as likes get inserted / deleted / updated to the LIKER table then the SELECT statement would be more simple and more efficient than it is now. I am asking, is it more efficient to have this complex query with the COUNTS or to have the extra columns and triggers?

And to generalise, is it more efficient to COUNT or to have an extra column for counting when being queried on a regular basis?

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

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

发布评论

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

评论(1

随心而道 2024-12-08 22:11:30

你的查询效率很低。您可以轻松消除这些子查询,这将显着提高性能:

您的两个子查询可以简单地替换为:

sum(liker) likes,
sum(abs(liker - 1)) dislikes,

使整个查询如下:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
    sum(liker) likes,
    sum(abs(liker - 1)) dislikes,
    comment_likers.liker
FROM comments
INNER JOIN usrs ON comments.usr_id = usrs.usr_id
LEFT JOIN comment_likers  ON comments.comment_id = comment_likers.comment_id 
  AND comment_likers.usr_id = $usrID
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;

Your query is very inefficient. You can easily eliminate those sub queries, which will dramatically increase performance:

Your two sub queries can be replaced by simply:

sum(liker) likes,
sum(abs(liker - 1)) dislikes,

Making the whole query this:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
    sum(liker) likes,
    sum(abs(liker - 1)) dislikes,
    comment_likers.liker
FROM comments
INNER JOIN usrs ON comments.usr_id = usrs.usr_id
LEFT JOIN comment_likers  ON comments.comment_id = comment_likers.comment_id 
  AND comment_likers.usr_id = $usrID
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文