From aad76a13e8381fc2a7c79d37d842d530c450a728 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 20 May 2020 09:34:55 +0530 Subject: [PATCH] MM-24170: Allow mysql to choose the right index (#14588) We use the same optimization used in MM-23369 to prevent mysql from using the index in the sort query. Co-authored-by: mattermod --- store/sqlstore/post_store.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 2702a9e89a..e203252ecd 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -714,12 +714,14 @@ func (s *SqlPostStore) getPostsAround(before bool, options model.GetPostsOptions sq.Eq{"ChannelId": options.ChannelId}, sq.Eq{"DeleteAt": int(0)}, }). - OrderBy("CreateAt " + sort). + // Adding ChannelId and DeleteAt order columns + // to let mysql choose the "idx_posts_channel_id_delete_at_create_at" index always. + // See MM-24170. + OrderBy("ChannelId", "DeleteAt", "CreateAt "+sort). Limit(uint64(options.PerPage)). Offset(uint64(offset)) queryString, args, err := query.ToSql() - if err != nil { return nil, model.NewAppError("SqlPostStore.GetPostContext", "store.sql_post.get_posts_around.get.app_error", nil, "channelId="+options.ChannelId+err.Error(), http.StatusInternalServerError) }