From c4c1fda1285f16f2b53531d282f2623b9bae3f5d Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Sat, 21 Aug 2021 07:05:52 +0200 Subject: [PATCH] [MM-36387] Address db performance audit items (#18078) --- store/sqlstore/channel_store.go | 14 ++++++++++---- store/sqlstore/post_store.go | 15 +++------------ store/sqlstore/status_store.go | 2 +- store/sqlstore/thread_store.go | 2 +- store/sqlstore/upgrade.go | 18 +++++++++++++++++- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 1aeb57eaa9..8cbc03475f 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -420,17 +420,20 @@ func newSqlChannelStore(sqlStore *SqlStore, metrics einterfaces.MetricsInterface } func (s SqlChannelStore) createIndexesIfNotExists() { - s.CreateIndexIfNotExists("idx_channels_team_id", "Channels", "TeamId") s.CreateIndexIfNotExists("idx_channels_update_at", "Channels", "UpdateAt") s.CreateIndexIfNotExists("idx_channels_create_at", "Channels", "CreateAt") s.CreateIndexIfNotExists("idx_channels_delete_at", "Channels", "DeleteAt") + s.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId") + s.CreateCompositeIndexIfNotExists("idx_channels_team_id_display_name", "Channels", []string{"TeamId", "DisplayName"}) + s.CreateCompositeIndexIfNotExists("idx_channels_team_id_type", "Channels", []string{"TeamId", "Type"}) if s.DriverName() == model.DatabaseDriverPostgres { s.CreateIndexIfNotExists("idx_channels_name_lower", "Channels", "lower(Name)") s.CreateIndexIfNotExists("idx_channels_displayname_lower", "Channels", "lower(DisplayName)") } - s.CreateIndexIfNotExists("idx_channelmembers_user_id", "ChannelMembers", "UserId") + s.CreateCompositeIndexIfNotExists("idx_channelmembers_user_id_channel_id_last_viewed_at", "ChannelMembers", []string{"UserId", "ChannelId", "LastViewedAt"}) + s.CreateCompositeIndexIfNotExists("idx_channelmembers_channel_id_scheme_guest_user_id", "ChannelMembers", []string{"ChannelId", "SchemeGuest", "UserId"}) s.CreateFullTextIndexIfNotExists("idx_channel_search_txt", "Channels", "Name, DisplayName, Purpose") @@ -441,7 +444,6 @@ func (s SqlChannelStore) createIndexesIfNotExists() { s.CreateIndexIfNotExists("idx_publicchannels_displayname_lower", "PublicChannels", "lower(DisplayName)") } s.CreateFullTextIndexIfNotExists("idx_publicchannels_search_txt", "PublicChannels", "Name, DisplayName, Purpose") - s.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId") } // MigratePublicChannels initializes the PublicChannels table with data created before this version @@ -1972,11 +1974,15 @@ func (s SqlChannelStore) InvalidateGuestCount(channelId string) { //nolint:unparam func (s SqlChannelStore) GetGuestCount(channelId string, allowFromCache bool) (int64, error) { + var indexHint string + if s.DriverName() == model.DatabaseDriverMysql { + indexHint = `USE INDEX(idx_channelmembers_channel_id_scheme_guest_user_id)` + } count, err := s.GetReplica().SelectInt(` SELECT count(*) FROM - ChannelMembers, + ChannelMembers `+indexHint+`, Users WHERE ChannelMembers.UserId = Users.Id diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 5df1a0a7eb..5051d59ae9 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -156,12 +156,12 @@ func (s *SqlPostStore) createIndexesIfNotExists() { s.CreateIndexIfNotExists("idx_posts_update_at", "Posts", "UpdateAt") s.CreateIndexIfNotExists("idx_posts_create_at", "Posts", "CreateAt") s.CreateIndexIfNotExists("idx_posts_delete_at", "Posts", "DeleteAt") - s.CreateIndexIfNotExists("idx_posts_root_id", "Posts", "RootId") s.CreateIndexIfNotExists("idx_posts_user_id", "Posts", "UserId") s.CreateIndexIfNotExists("idx_posts_is_pinned", "Posts", "IsPinned") s.CreateCompositeIndexIfNotExists("idx_posts_channel_id_update_at", "Posts", []string{"ChannelId", "UpdateAt"}) s.CreateCompositeIndexIfNotExists("idx_posts_channel_id_delete_at_create_at", "Posts", []string{"ChannelId", "DeleteAt", "CreateAt"}) + s.CreateCompositeIndexIfNotExists("idx_posts_root_id_delete_at", "Posts", []string{"RootId", "DeleteAt"}) s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message") s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags") @@ -2367,18 +2367,9 @@ func (s *SqlPostStore) cleanupThreads(postId, rootId string, permanent bool) err return nil } if rootId != "" { - thread, err := s.Thread().Get(rootId) + _, err := s.GetMaster().Exec(`UPDATE Threads SET ReplyCount = ReplyCount - 1 WHERE PostId = :Id AND ReplyCount > 0`, map[string]interface{}{"Id": rootId}) if err != nil { - var nfErr *store.ErrNotFound - if !errors.As(err, &nfErr) { - return errors.Wrap(err, "failed to get a thread") - } - } - if thread != nil { - thread.ReplyCount -= 1 - if _, err = s.Thread().Update(thread); err != nil { - return errors.Wrap(err, "failed to update thread") - } + return errors.Wrap(err, "failed to update Threads") } } return nil diff --git a/store/sqlstore/status_store.go b/store/sqlstore/status_store.go index 479c755c8c..2b6bed4c72 100644 --- a/store/sqlstore/status_store.go +++ b/store/sqlstore/status_store.go @@ -35,7 +35,7 @@ func newSqlStatusStore(sqlStore *SqlStore) store.StatusStore { } func (s SqlStatusStore) createIndexesIfNotExists() { - s.CreateIndexIfNotExists("idx_status_status", "Status", "Status") + s.CreateCompositeIndexIfNotExists("idx_status_status_dndendtime", "Status", []string{"Status", "DNDEndTime"}) } func (s SqlStatusStore) SaveOrUpdate(st *model.Status) error { diff --git a/store/sqlstore/thread_store.go b/store/sqlstore/thread_store.go index 43a5534d7c..8d7d8108e7 100644 --- a/store/sqlstore/thread_store.go +++ b/store/sqlstore/thread_store.go @@ -61,7 +61,7 @@ func (s *SqlThreadStore) createIndexesIfNotExists() { s.CreateIndexIfNotExists("idx_thread_memberships_last_update_at", "ThreadMemberships", "LastUpdated") s.CreateIndexIfNotExists("idx_thread_memberships_last_view_at", "ThreadMemberships", "LastViewed") s.CreateIndexIfNotExists("idx_thread_memberships_user_id", "ThreadMemberships", "UserId") - s.CreateIndexIfNotExists("idx_threads_channel_id", "Threads", "ChannelId") + s.CreateCompositeIndexIfNotExists("idx_threads_channel_id_last_reply_at", "Threads", []string{"ChannelId", "LastReplyAt"}) } func (s *SqlThreadStore) SaveMultiple(threads []*model.Thread) ([]*model.Thread, int, error) { diff --git a/store/sqlstore/upgrade.go b/store/sqlstore/upgrade.go index 0e1873674e..43bedc6f99 100644 --- a/store/sqlstore/upgrade.go +++ b/store/sqlstore/upgrade.go @@ -818,7 +818,6 @@ func upgradeDatabaseToVersion522(sqlStore *SqlStore) { if shouldPerformUpgrade(sqlStore, Version5210, Version5220) { sqlStore.CreateIndexIfNotExists("idx_teams_scheme_id", "Teams", "SchemeId") sqlStore.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId") - sqlStore.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId") sqlStore.CreateIndexIfNotExists("idx_schemes_channel_guest_role", "Schemes", "DefaultChannelGuestRole") sqlStore.CreateIndexIfNotExists("idx_schemes_channel_user_role", "Schemes", "DefaultChannelUserRole") sqlStore.CreateIndexIfNotExists("idx_schemes_channel_admin_role", "Schemes", "DefaultChannelAdminRole") @@ -1329,6 +1328,23 @@ func upgradeDatabaseToVersion600(sqlStore *SqlStore) { sqlStore.GetMaster().ExecNoTimeout("UPDATE CommandWebhooks SET RootId = ParentId WHERE RootId = '' AND RootId != ParentId") sqlStore.RemoveColumnIfExists("CommandWebhooks", "ParentId") + sqlStore.CreateCompositeIndexIfNotExists("idx_posts_root_id_delete_at", "Posts", []string{"RootId", "DeleteAt"}) + sqlStore.RemoveIndexIfExists("idx_posts_root_id", "Posts") + + sqlStore.CreateCompositeIndexIfNotExists("idx_channels_team_id_display_name", "Channels", []string{"TeamId", "DisplayName"}) + sqlStore.CreateCompositeIndexIfNotExists("idx_channels_team_id_type", "Channels", []string{"TeamId", "Type"}) + sqlStore.RemoveIndexIfExists("idx_channels_team_id", "Channels") + + sqlStore.CreateCompositeIndexIfNotExists("idx_threads_channel_id_last_reply_at", "Threads", []string{"ChannelId", "LastReplyAt"}) + sqlStore.RemoveIndexIfExists("idx_threads_channel_id", "Threads") + + sqlStore.CreateCompositeIndexIfNotExists("idx_channelmembers_user_id_channel_id_last_viewed_at", "ChannelMembers", []string{"UserId", "ChannelId", "LastViewedAt"}) + sqlStore.CreateCompositeIndexIfNotExists("idx_channelmembers_channel_id_scheme_guest_user_id", "ChannelMembers", []string{"ChannelId", "SchemeGuest", "UserId"}) + sqlStore.RemoveIndexIfExists("idx_channelmembers_user_id", "ChannelMembers") + + sqlStore.CreateCompositeIndexIfNotExists("idx_status_status_dndendtime", "Status", []string{"Status", "DNDEndTime"}) + sqlStore.RemoveIndexIfExists("idx_status_status", "Status") + if sqlStore.DriverName() == model.DatabaseDriverPostgres { sqlStore.AlterColumnTypeIfExists("Posts", "FileIds", "text", "varchar(300)") }