From bb9ac216e32cb49698614337396835dedb474b21 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 17 Dec 2023 16:39:58 +0100 Subject: [PATCH] Avoid `WHERE IN` for comment migration query - Rewrite `UpdateCommentsMigrationsByType` to not use `WHERE IN` as that's a performance diaster for MariaDB, it now use batching to query the the relevant comment IDs via JOINs (which is not possible in a UPDATE query for SQLite) and then update them in a seperate query. - Resolves https://codeberg.org/forgejo/forgejo/issues/1856 --- models/issues/comment.go | 46 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index ba5aed9c652e9..be209c867d23f 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -1160,22 +1160,36 @@ func DeleteComment(ctx context.Context, comment *Comment) error { // UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id func UpdateCommentsMigrationsByType(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { - _, err := db.GetEngine(ctx).Table("comment"). - Where(builder.In("issue_id", - builder.Select("issue.id"). - From("issue"). - InnerJoin("repository", "issue.repo_id = repository.id"). - Where(builder.Eq{ - "repository.original_service_type": tp, - }), - )). - And("comment.original_author_id = ?", originalAuthorID). - Update(map[string]any{ - "poster_id": posterID, - "original_author": "", - "original_author_id": 0, - }) - return err + const batchSize = 50 + for { + commentIDs := make([]int64, 0, batchSize) + if err := db.GetEngine(ctx). + Table("comment"). + Select("`comment`.id"). + Join("INNER", "issue", "`comment`.issue_id = `issue`.id"). + Join("INNER", "repository", "`repository`.id = `issue`.repo_id"). + Where("`repository`.original_service_type = ? AND `comment`.original_author_id = ?", tp, originalAuthorID). + Limit(batchSize, 0). + Find(&commentIDs); err != nil { + return err + } + + for _, commentID := range commentIDs { + if _, err := db.GetEngine(ctx).Table(&Comment{}).ID(commentID).Update(map[string]any{ + "poster_id": posterID, + "original_author": "", + "original_author_id": 0, + }); err != nil { + return err + } + } + + if len(commentIDs) < batchSize { + break + } + } + + return nil } // CreateAutoMergeComment is a internal function, only use it for CommentTypePRScheduledToAutoMerge and CommentTypePRUnScheduledToAutoMerge CommentTypes