diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 817eb3d96e..e454584cc0 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -770,7 +770,10 @@ func (s *SqlPostStore) getPostIdAroundTime(channelId string, time int64, before sq.Eq{"ChannelId": 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-23369. + OrderBy("ChannelId", "DeleteAt", "CreateAt "+sort). Limit(1) queryString, args, err := query.ToSql() @@ -797,7 +800,10 @@ func (s *SqlPostStore) GetPostAfterTime(channelId string, time int64) (*model.Po sq.Eq{"ChannelId": channelId}, sq.Eq{"DeleteAt": int(0)}, }). - OrderBy("CreateAt ASC"). + // Adding ChannelId and DeleteAt order columns + // to let mysql choose the "idx_posts_channel_id_delete_at_create_at" index always. + // See MM-23369. + OrderBy("ChannelId", "DeleteAt", "CreateAt ASC"). Limit(1) queryString, args, err := query.ToSql()