From e321b40bb0c5557bc25f7e001e5230f04809e4e3 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 2 Jul 2022 14:31:51 +0100 Subject: [PATCH] Refix indices on actions table (#20158) (#20198) Backport #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 --- models/action.go | 6 ++--- models/migrations/migrations.go | 9 +++++-- models/migrations/v199.go | 9 +------ models/migrations/v216.go | 42 ++---------------------------- models/migrations/v218.go | 46 +++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 models/migrations/v218.go diff --git a/models/action.go b/models/action.go index 78cc93e1a2..7dbdaa8530 100644 --- a/models/action.go +++ b/models/action.go @@ -92,12 +92,12 @@ func init() { // TableIndices implements xorm's TableIndices interface func (a *Action) TableIndices() []*schemas.Index { + repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType) + repoIndex.AddColumn("repo_id", "user_id", "is_deleted") + 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} } diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index edd4beb451..0d35ac78d3 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -56,6 +56,9 @@ type Version struct { Version int64 } +// Use noopMigration when there is a migration that has been no-oped +var noopMigration = func(_ *xorm.Engine) error { return nil } + // This is a sequence of migrations. Add new migrations to the bottom of the list. // If you want to "retire" a migration, remove it from the top of the list and // update minDBVersion accordingly @@ -351,7 +354,7 @@ var migrations = []Migration{ // v198 -> v199 NewMigration("Add issue content history table", addTableIssueContentHistory), // v199 -> v200 - NewMigration("No-op (remote version is using AppState now)", addRemoteVersionTableNoop), + NewMigration("No-op (remote version is using AppState now)", noopMigration), // v200 -> v201 NewMigration("Add table app_state", addTableAppState), // v201 -> v202 @@ -388,9 +391,11 @@ var migrations = []Migration{ // v215 -> v216 NewMigration("allow to view files in PRs", addReviewViewedFiles), // v216 -> v217 - NewMigration("Improve Action table indices", improveActionTableIndices), + NewMigration("No-op (Improve Action table indices v1)", noopMigration), // v217 -> v218 NewMigration("Alter hook_task table TEXT fields to LONGTEXT", alterHookTaskTextFieldsToLongText), + // v218 -> v219 + NewMigration("Improve Action table indices v2", improveActionTableIndices), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v199.go b/models/migrations/v199.go index 4351ba4fa8..29f9d49dbe 100644 --- a/models/migrations/v199.go +++ b/models/migrations/v199.go @@ -4,11 +4,4 @@ package migrations -import ( - "xorm.io/xorm" -) - -func addRemoteVersionTableNoop(x *xorm.Engine) error { - // 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. - return nil -} +// 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 --git a/models/migrations/v216.go b/models/migrations/v216.go index 67c360016d..ab44808402 100644 --- a/models/migrations/v216.go +++ b/models/migrations/v216.go @@ -4,43 +4,5 @@ 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 { - return x.Sync2(&improveActionTableIndicesAction{}) -} +// This migration added non-ideal indices to the action table which on larger datasets slowed things down +// it has been superceded by v218.go diff --git a/models/migrations/v218.go b/models/migrations/v218.go new file mode 100644 index 0000000000..dee8e5517e --- /dev/null +++ b/models/migrations/v218.go @@ -0,0 +1,46 @@ +// 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" + "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 (*improveActionTableIndicesAction) TableName() string { + return "action" +} + +// TableIndices implements xorm's TableIndices interface +func (*improveActionTableIndicesAction) TableIndices() []*schemas.Index { + repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType) + repoIndex.AddColumn("repo_id", "user_id", "is_deleted") + + actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType) + actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted") + + return []*schemas.Index{actUserIndex, repoIndex} +} + +func improveActionTableIndices(x *xorm.Engine) error { + return x.Sync2(&improveActionTableIndicesAction{}) +}