mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-17 01:20:46 +08:00
feat(posts): 优化帖子评论统计性能
- 在 Post 模型中添加 commentCount 和 lastReplyAt 字段 - 在 CommentService 中实现更新帖子评论统计的方法 - 在 PostMapper 中使用 Post 模型中的评论统计字段 - 新增数据库迁移脚本,添加评论统计字段和索引 - 更新相关测试用例
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
-- Add comment count and last reply time fields to posts table for performance optimization
|
||||
ALTER TABLE posts ADD COLUMN comment_count BIGINT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE posts ADD COLUMN last_reply_at DATETIME(6) NULL;
|
||||
|
||||
-- Add index on last_reply_at for sorting by latest reply
|
||||
CREATE INDEX idx_posts_last_reply_at ON posts(last_reply_at);
|
||||
|
||||
-- Initialize comment_count and last_reply_at with existing data
|
||||
UPDATE posts p SET
|
||||
comment_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM comments c
|
||||
WHERE c.post_id = p.id AND c.deleted_at IS NULL
|
||||
),
|
||||
last_reply_at = (
|
||||
SELECT MAX(c.created_at)
|
||||
FROM comments c
|
||||
WHERE c.post_id = p.id AND c.deleted_at IS NULL
|
||||
);
|
||||
Reference in New Issue
Block a user