From 133c63ec1e146f946bb5fba2636956259e0260a3 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 23 Apr 2022 18:17:01 +0100 Subject: [PATCH 1/7] 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 #16665 Signed-off-by: Andrew Thornton --- models/action.go | 12 ++++++------ models/migrations/migrations.go | 2 ++ models/migrations/v213.go | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 models/migrations/v213.go diff --git a/models/action.go b/models/action.go index 3339d3b87af2a..24a6099633479 100644 --- a/models/action.go +++ b/models/action.go @@ -68,19 +68,19 @@ const ( // used in template render. type Action struct { ID int64 `xorm:"pk autoincr"` - UserID int64 `xorm:"INDEX"` // Receiver user id. + UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. OpType ActionType - ActUserID int64 `xorm:"INDEX"` // Action user id. + ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. ActUser *user_model.User `xorm:"-"` - RepoID int64 `xorm:"INDEX"` + RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` Repo *repo_model.Repository `xorm:"-"` CommentID int64 `xorm:"INDEX"` Comment *Comment `xorm:"-"` - IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsDeleted bool `xorm:"NOT NULL DEFAULT false"` RefName string - IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsPrivate bool `xorm:"NOT NULL DEFAULT false"` Content string `xorm:"TEXT"` - CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` } func init() { diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index de1d41e71a848..995a16641386e 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -380,6 +380,8 @@ var migrations = []Migration{ NewMigration("Create ForeignReference table", createForeignReferenceTable), // v212 -> v213 NewMigration("Add package tables", addPackageTables), + // v213 -> v214 + NewMigration("Improve Action table indices", improveActionTableIndices), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v213.go b/models/migrations/v213.go new file mode 100644 index 0000000000000..bcfca510c85b2 --- /dev/null +++ b/models/migrations/v213.go @@ -0,0 +1,27 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "code.gitea.io/gitea/modules/timeutil" + "xorm.io/xorm" +) + +func improveActionTableIndices(x *xorm.Engine) error { + type Action struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. + OpType int + ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. + RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` + CommentID int64 `xorm:"INDEX"` + IsDeleted bool `xorm:"NOT NULL DEFAULT false"` + RefName string + IsPrivate bool `xorm:"NOT NULL DEFAULT false"` + Content string `xorm:"TEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` + } + return x.Sync2(&Action{}) +} From 2f2234e63e7aedc53afcfac98898a6dbf815034f Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 4 May 2022 22:23:47 +0100 Subject: [PATCH 2/7] move for merge Signed-off-by: Andrew Thornton --- models/migrations/{v213.go => v214.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/{v213.go => v214.go} (100%) diff --git a/models/migrations/v213.go b/models/migrations/v214.go similarity index 100% rename from models/migrations/v213.go rename to models/migrations/v214.go From 4a2c406c5c71793073c999253d9e2aac89c9e648 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 20 May 2022 20:50:28 +0100 Subject: [PATCH 3/7] pre-merge Signed-off-by: Andrew Thornton --- models/migrations/{v214.go => v216.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/{v214.go => v216.go} (100%) diff --git a/models/migrations/v214.go b/models/migrations/v216.go similarity index 100% rename from models/migrations/v214.go rename to models/migrations/v216.go From 9d186bde3b8704358b24230730034014b19a7798 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 20 May 2022 21:02:16 +0100 Subject: [PATCH 4/7] Drop indexes in migration Signed-off-by: Andrew Thornton --- models/migrations/v216.go | 49 ++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/models/migrations/v216.go b/models/migrations/v216.go index bcfca510c85b2..9e3c7462af8d0 100644 --- a/models/migrations/v216.go +++ b/models/migrations/v216.go @@ -10,18 +10,41 @@ import ( ) func improveActionTableIndices(x *xorm.Engine) error { - type Action struct { - ID int64 `xorm:"pk autoincr"` - UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. - OpType int - ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. - RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` - CommentID int64 `xorm:"INDEX"` - IsDeleted bool `xorm:"NOT NULL DEFAULT false"` - RefName string - IsPrivate bool `xorm:"NOT NULL DEFAULT false"` - Content string `xorm:"TEXT"` - CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` + { + type Action struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 `xorm:"INDEX"` // Receiver user id. + OpType int + ActUserID int64 `xorm:"INDEX"` // Action user id. + RepoID int64 `xorm:"INDEX"` + CommentID int64 `xorm:"INDEX"` + IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"` + RefName string + IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"` + Content string `xorm:"TEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + } + if err := x.Sync2(&Action{}); err != nil { + return err + } + if err := x.DropIndexes(&Action{}); err != nil { + return err + } + } + { + type Action struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. + OpType int + ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. + RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` + CommentID int64 `xorm:"INDEX"` + IsDeleted bool `xorm:"NOT NULL DEFAULT false"` + RefName string + IsPrivate bool `xorm:"NOT NULL DEFAULT false"` + Content string `xorm:"TEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` + } + return x.Sync2(&Action{}) } - return x.Sync2(&Action{}) } From a1f182b1096f5598ee8a4aac0c4d7a14431a4e3e Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 4 Jun 2022 16:44:13 +0100 Subject: [PATCH 5/7] Update models/migrations/migrations.go Co-authored-by: delvh --- models/migrations/migrations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 5d9e1a2b4e387..2b0bc4787a96e 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -387,7 +387,7 @@ var migrations = []Migration{ NewMigration("Add auto merge table", addAutoMergeTable), // v215 -> v216 NewMigration("allow to view files in PRs", addReviewViewedFiles), - // v215 -> v217 + // v216 -> v217 NewMigration("Improve Action table indices", improveActionTableIndices), } From de86bd4a46f2eabb87e5d914da29b7933a420ae9 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 17 Jun 2022 05:36:19 +0100 Subject: [PATCH 6/7] Use TableIndices to improve things Signed-off-by: Andrew Thornton --- models/action.go | 22 ++++++++++++++---- models/migrations/v216.go | 48 ++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/models/action.go b/models/action.go index f8dca6b383e02..78cc93e1a2144 100644 --- a/models/action.go +++ b/models/action.go @@ -30,6 +30,7 @@ import ( "code.gitea.io/gitea/modules/util" "xorm.io/builder" + "xorm.io/xorm/schemas" ) // ActionType represents the type of an action. @@ -70,11 +71,11 @@ const ( // used in template render. type Action struct { ID int64 `xorm:"pk autoincr"` - UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. + UserID int64 // Receiver user id. OpType ActionType - ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. - ActUser *user_model.User `xorm:"-"` - RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` + ActUserID int64 // Action user id. + ActUser *user_model.User `xorm:"-"` + RepoID int64 Repo *repo_model.Repository `xorm:"-"` CommentID int64 `xorm:"INDEX"` Comment *issues_model.Comment `xorm:"-"` @@ -82,13 +83,24 @@ type Action struct { RefName string IsPrivate bool `xorm:"NOT NULL DEFAULT false"` Content string `xorm:"TEXT"` - CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` } func init() { db.RegisterModel(new(Action)) } +// TableIndices implements xorm's TableIndices interface +func (a *Action) TableIndices() []*schemas.Index { + actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType) + actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted") + + repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType) + repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted") + + return []*schemas.Index{actUserIndex, repoIndex} +} + // GetOpType gets the ActionType of this action. func (a *Action) GetOpType() ActionType { return a.OpType diff --git a/models/migrations/v216.go b/models/migrations/v216.go index 9e3c7462af8d0..a3b6cadec3670 100644 --- a/models/migrations/v216.go +++ b/models/migrations/v216.go @@ -7,8 +7,39 @@ package migrations import ( "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" + "xorm.io/xorm/schemas" ) +type improveActionTableIndicesAction struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 // Receiver user id. + OpType int + ActUserID int64 // Action user id. + RepoID int64 + CommentID int64 `xorm:"INDEX"` + IsDeleted bool `xorm:"NOT NULL DEFAULT false"` + RefName string + IsPrivate bool `xorm:"NOT NULL DEFAULT false"` + Content string `xorm:"TEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` +} + +// TableName sets the name of this table +func (a *improveActionTableIndicesAction) TableName() string { + return "action" +} + +// TableIndices implements xorm's TableIndices interface +func (a *improveActionTableIndicesAction) TableIndices() []*schemas.Index { + actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType) + actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted") + + repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType) + repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted") + + return []*schemas.Index{actUserIndex, repoIndex} +} + func improveActionTableIndices(x *xorm.Engine) error { { type Action struct { @@ -31,20 +62,5 @@ func improveActionTableIndices(x *xorm.Engine) error { return err } } - { - type Action struct { - ID int64 `xorm:"pk autoincr"` - UserID int64 `xorm:"INDEX(u_ua_and_r)"` // Receiver user id. - OpType int - ActUserID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r)"` // Action user id. - RepoID int64 `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r)"` - CommentID int64 `xorm:"INDEX"` - IsDeleted bool `xorm:"NOT NULL DEFAULT false"` - RefName string - IsPrivate bool `xorm:"NOT NULL DEFAULT false"` - Content string `xorm:"TEXT"` - CreatedUnix timeutil.TimeStamp `xorm:"INDEX(u_ua_and_r) INDEX(ua_and_r) INDEX(r) created"` - } - return x.Sync2(&Action{}) - } + return x.Sync2(&improveActionTableIndicesAction{}) } From 4b32d1998bd2e22e048d3055d57a40b3c55e1b14 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 18 Jun 2022 08:05:53 +0100 Subject: [PATCH 7/7] Update models/migrations/v216.go --- models/migrations/v216.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/migrations/v216.go b/models/migrations/v216.go index a3b6cadec3670..b011c11d95e32 100644 --- a/models/migrations/v216.go +++ b/models/migrations/v216.go @@ -6,6 +6,7 @@ package migrations import ( "code.gitea.io/gitea/modules/timeutil" + "xorm.io/xorm" "xorm.io/xorm/schemas" )