Skip to content

Commit 5c9c0b8

Browse files
authored
Refix indices on actions table (#20158)
Unforunately the previous PR #20035 created indices that were not helpful for SQLite. This PR adjusts these after testing using the try.gitea.io db. Fix #20129 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 54e7483 commit 5c9c0b8

File tree

5 files changed

+59
-53
lines changed

5 files changed

+59
-53
lines changed

Diff for: models/action.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ func init() {
9292

9393
// TableIndices implements xorm's TableIndices interface
9494
func (a *Action) TableIndices() []*schemas.Index {
95+
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
96+
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")
97+
9598
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
9699
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
97100

98-
repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
99-
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")
100-
101101
return []*schemas.Index{actUserIndex, repoIndex}
102102
}
103103

Diff for: models/migrations/migrations.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type Version struct {
5656
Version int64
5757
}
5858

59+
// Use noopMigration when there is a migration that has been no-oped
60+
var noopMigration = func(_ *xorm.Engine) error { return nil }
61+
5962
// This is a sequence of migrations. Add new migrations to the bottom of the list.
6063
// If you want to "retire" a migration, remove it from the top of the list and
6164
// update minDBVersion accordingly
@@ -351,7 +354,7 @@ var migrations = []Migration{
351354
// v198 -> v199
352355
NewMigration("Add issue content history table", addTableIssueContentHistory),
353356
// v199 -> v200
354-
NewMigration("No-op (remote version is using AppState now)", addRemoteVersionTableNoop),
357+
NewMigration("No-op (remote version is using AppState now)", noopMigration),
355358
// v200 -> v201
356359
NewMigration("Add table app_state", addTableAppState),
357360
// v201 -> v202
@@ -388,9 +391,11 @@ var migrations = []Migration{
388391
// v215 -> v216
389392
NewMigration("allow to view files in PRs", addReviewViewedFiles),
390393
// v216 -> v217
391-
NewMigration("Improve Action table indices", improveActionTableIndices),
394+
NewMigration("No-op (Improve Action table indices v1)", noopMigration),
392395
// v217 -> v218
393396
NewMigration("Alter hook_task table TEXT fields to LONGTEXT", alterHookTaskTextFieldsToLongText),
397+
// v218 -> v219
398+
NewMigration("Improve Action table indices v2", improveActionTableIndices),
394399
}
395400

396401
// GetCurrentDBVersion returns the current db version

Diff for: models/migrations/v199.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,4 @@
44

55
package migrations
66

7-
import (
8-
"xorm.io/xorm"
9-
)
10-
11-
func addRemoteVersionTableNoop(x *xorm.Engine) error {
12-
// we used to use a table `remote_version` to store information for updater, now we use `AppState`, so this migration task is a no-op now.
13-
return nil
14-
}
7+
// We used to use a table `remote_version` to store information for updater, now we use `AppState`, so this migration task is a no-op now.

Diff for: models/migrations/v216.go

+2-40
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,5 @@
44

55
package migrations
66

7-
import (
8-
"code.gitea.io/gitea/modules/timeutil"
9-
10-
"xorm.io/xorm"
11-
"xorm.io/xorm/schemas"
12-
)
13-
14-
type improveActionTableIndicesAction struct {
15-
ID int64 `xorm:"pk autoincr"`
16-
UserID int64 // Receiver user id.
17-
OpType int
18-
ActUserID int64 // Action user id.
19-
RepoID int64
20-
CommentID int64 `xorm:"INDEX"`
21-
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
22-
RefName string
23-
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
24-
Content string `xorm:"TEXT"`
25-
CreatedUnix timeutil.TimeStamp `xorm:"created"`
26-
}
27-
28-
// TableName sets the name of this table
29-
func (a *improveActionTableIndicesAction) TableName() string {
30-
return "action"
31-
}
32-
33-
// TableIndices implements xorm's TableIndices interface
34-
func (a *improveActionTableIndicesAction) TableIndices() []*schemas.Index {
35-
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
36-
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
37-
38-
repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
39-
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")
40-
41-
return []*schemas.Index{actUserIndex, repoIndex}
42-
}
43-
44-
func improveActionTableIndices(x *xorm.Engine) error {
45-
return x.Sync2(&improveActionTableIndicesAction{})
46-
}
7+
// This migration added non-ideal indices to the action table which on larger datasets slowed things down
8+
// it has been superceded by v218.go

Diff for: models/migrations/v218.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
10+
"xorm.io/xorm"
11+
"xorm.io/xorm/schemas"
12+
)
13+
14+
type improveActionTableIndicesAction struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
UserID int64 // Receiver user id.
17+
OpType int
18+
ActUserID int64 // Action user id.
19+
RepoID int64
20+
CommentID int64 `xorm:"INDEX"`
21+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
22+
RefName string
23+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
24+
Content string `xorm:"TEXT"`
25+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
26+
}
27+
28+
// TableName sets the name of this table
29+
func (*improveActionTableIndicesAction) TableName() string {
30+
return "action"
31+
}
32+
33+
// TableIndices implements xorm's TableIndices interface
34+
func (*improveActionTableIndicesAction) TableIndices() []*schemas.Index {
35+
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
36+
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")
37+
38+
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
39+
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
40+
41+
return []*schemas.Index{actUserIndex, repoIndex}
42+
}
43+
44+
func improveActionTableIndices(x *xorm.Engine) error {
45+
return x.Sync2(&improveActionTableIndicesAction{})
46+
}

0 commit comments

Comments
 (0)