1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-08-01 17:38:33 +00:00

[GITEA] Use join for the deleting issue actions query

- The action tables can become very large as it's a dumpster for every
action that an user does on an repository.
- The following query: `DELETE FROM action WHERE comment_id IN (SELECT id FROM comment WHERE
issue_id=?)` is not using indexes for `comment_id` and is instead using
an full table scan by MariaDB.
- Rewriting the query to use an JOIN will allow MariaDB to use the
index.
- More information: https://codeberg.org/Codeberg-Infrastructure/techstack-support/issues/9

(cherry picked from commit 6646fcf31f)

Conflicts:
	tests/integration/api_nodeinfo_test.go
	https://codeberg.org/forgejo/forgejo/pulls/1178
This commit is contained in:
Gusted 2023-07-29 16:59:23 +02:00 committed by Earl Warren
parent b977910bab
commit 6465b74632
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
6 changed files with 45 additions and 7 deletions

View file

@ -687,11 +687,21 @@ func NotifyWatchersActions(acts []*Action) error {
// DeleteIssueActions delete all actions related with issueID
func DeleteIssueActions(ctx context.Context, repoID, issueID int64) error {
// delete actions assigned to this issue
subQuery := builder.Select("`id`").
From("`comment`").
Where(builder.Eq{"`issue_id`": issueID})
if _, err := db.GetEngine(ctx).In("comment_id", subQuery).Delete(&Action{}); err != nil {
return err
// MySQL doesn't use the indexes on comment_id when using a subquery.
// It does uses the indexes when using an JOIN, however SQLite doesn't
// allow JOINs in DELETE statements and XORM doesn't allow them as well.
// So, an specific raw SQL query for MySQL so the query makes use of indexes.
if setting.Database.Type.IsMySQL() {
if _, err := db.GetEngine(ctx).Exec("DELETE action FROM action JOIN comment ON action.comment_id = comment.id WHERE comment.issue_id = ?", issueID); err != nil {
return err
}
} else {
subQuery := builder.Select("`id`").From("`comment`").
Where(builder.Eq{"`issue_id`": issueID})
if _, err := db.GetEngine(ctx).In("comment_id", subQuery).Delete(&Action{}); err != nil {
return err
}
}
_, err := db.GetEngine(ctx).Table("action").Where("repo_id = ?", repoID).