Skip to content

Commit 133c63e

Browse files
committed
Improve action table indices
Improve the indices on the action table by creating a covering index that covers the common queries and removes unhelpful indices. Fix go-gitea#16665 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 9550e5a commit 133c63e

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

models/action.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ const (
6868
// used in template render.
6969
type Action struct {
7070
ID int64 `xorm:"pk autoincr"`
71-
UserID int64 `xorm:"INDEX"` // Receiver user id.
71+
UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id.
7272
OpType ActionType
73-
ActUserID int64 `xorm:"INDEX"` // Action user id.
73+
ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id.
7474
ActUser *user_model.User `xorm:"-"`
75-
RepoID int64 `xorm:"INDEX"`
75+
RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"`
7676
Repo *repo_model.Repository `xorm:"-"`
7777
CommentID int64 `xorm:"INDEX"`
7878
Comment *Comment `xorm:"-"`
79-
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
79+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
8080
RefName string
81-
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
81+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
8282
Content string `xorm:"TEXT"`
83-
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
83+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"`
8484
}
8585

8686
func init() {

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ var migrations = []Migration{
380380
NewMigration("Create ForeignReference table", createForeignReferenceTable),
381381
// v212 -> v213
382382
NewMigration("Add package tables", addPackageTables),
383+
// v213 -> v214
384+
NewMigration("Improve Action table indices", improveActionTableIndices),
383385
}
384386

385387
// GetCurrentDBVersion returns the current db version

models/migrations/v213.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"code.gitea.io/gitea/modules/timeutil"
9+
"xorm.io/xorm"
10+
)
11+
12+
func improveActionTableIndices(x *xorm.Engine) error {
13+
type Action struct {
14+
ID int64 `xorm:"pk autoincr"`
15+
UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id.
16+
OpType int
17+
ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id.
18+
RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"`
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:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"`
25+
}
26+
return x.Sync2(&Action{})
27+
}

0 commit comments

Comments
 (0)