Skip to content

Commit 913be9e

Browse files
authored
Add new index for action to resolve the performance problem (#32333)
Fix #32224
1 parent 7adc471 commit 913be9e

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

models/activities/action.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ func (a *Action) TableIndices() []*schemas.Index {
171171
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
172172
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
173173

174-
indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex}
174+
cuIndex := schemas.NewIndex("c_u", schemas.IndexType)
175+
cuIndex.AddColumn("user_id", "is_deleted")
176+
177+
indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex, cuIndex}
175178

176179
return indices
177180
}

models/migrations/migrations.go

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ func prepareMigrationTasks() []*migration {
365365
newMigration(305, "Add Repository Licenses", v1_23.AddRepositoryLicenses),
366366
newMigration(306, "Add BlockAdminMergeOverride to ProtectedBranch", v1_23.AddBlockAdminMergeOverrideBranchProtection),
367367
newMigration(307, "Fix milestone deadline_unix when there is no due date", v1_23.FixMilestoneNoDueDate),
368+
newMigration(308, "Add index(user_id, is_deleted) for action table", v1_23.AddNewIndexForUserDashboard),
368369
}
369370
return preparedMigrations
370371
}

models/migrations/v1_23/v308.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/timeutil"
8+
9+
"xorm.io/xorm"
10+
"xorm.io/xorm/schemas"
11+
)
12+
13+
type improveActionTableIndicesAction struct {
14+
ID int64 `xorm:"pk autoincr"`
15+
UserID int64 `xorm:"INDEX"` // Receiver user id.
16+
OpType int
17+
ActUserID int64 // Action user id.
18+
RepoID int64
19+
CommentID int64 `xorm:"INDEX"`
20+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
21+
RefName string
22+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
23+
Content string `xorm:"TEXT"`
24+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
25+
}
26+
27+
// TableName sets the name of this table
28+
func (*improveActionTableIndicesAction) TableName() string {
29+
return "action"
30+
}
31+
32+
func (a *improveActionTableIndicesAction) TableIndices() []*schemas.Index {
33+
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
34+
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")
35+
36+
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
37+
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
38+
39+
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
40+
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
41+
42+
cuIndex := schemas.NewIndex("c_u", schemas.IndexType)
43+
cuIndex.AddColumn("user_id", "is_deleted")
44+
45+
indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex, cuIndex}
46+
47+
return indices
48+
}
49+
50+
func AddNewIndexForUserDashboard(x *xorm.Engine) error {
51+
return x.Sync(new(improveActionTableIndicesAction))
52+
}

0 commit comments

Comments
 (0)