[MM-36387] Address db performance audit items (#18078)

Этот коммит содержится в:
Claudio Costa
2021-08-21 07:05:52 +02:00
коммит произвёл GitHub
родитель f13ebe84ef
Коммит c4c1fda128
5 изменённых файлов: 32 добавлений и 19 удалений

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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 {

Просмотреть файл

@@ -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) {

Просмотреть файл

@@ -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)")
}