diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index a14b7d15e4..fba4614a94 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -708,10 +708,18 @@ func (s *SqlPostStore) getPostsAround(before bool, options model.GetPostsOptions direction = ">" sort = "ASC" } + table := "Posts p" + // We force MySQL to use the right index to prevent it from accidentally + // using the index_merge_intersection optimization. + // See MM-27575. + if s.DriverName() == model.DATABASE_DRIVER_MYSQL { + table += " USE INDEX(idx_posts_channel_id_delete_at_create_at)" + } + replyCountSubQuery := s.getQueryBuilder().Select("COUNT(Posts.Id)").From("Posts").Where(sq.Expr("Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0")) query := s.getQueryBuilder().Select("p.*") query = query.Column(sq.Alias(replyCountSubQuery, "ReplyCount")) - query = query.From("Posts p"). + query = query.From(table). Where(sq.And{ sq.Expr(`CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = ?)`, options.PostId), sq.Eq{"ChannelId": options.ChannelId}, @@ -811,9 +819,17 @@ func (s *SqlPostStore) getPostIdAroundTime(channelId string, time int64, before sort = "ASC" } + table := "Posts" + // We force MySQL to use the right index to prevent it from accidentally + // using the index_merge_intersection optimization. + // See MM-27575. + if s.DriverName() == model.DATABASE_DRIVER_MYSQL { + table += " USE INDEX(idx_posts_channel_id_delete_at_create_at)" + } + query := s.getQueryBuilder(). Select("Id"). - From("Posts"). + From(table). Where(sq.And{ direction, sq.Eq{"ChannelId": channelId}, @@ -841,9 +857,17 @@ func (s *SqlPostStore) getPostIdAroundTime(channelId string, time int64, before } func (s *SqlPostStore) GetPostAfterTime(channelId string, time int64) (*model.Post, *model.AppError) { + table := "Posts" + // We force MySQL to use the right index to prevent it from accidentally + // using the index_merge_intersection optimization. + // See MM-27575. + if s.DriverName() == model.DATABASE_DRIVER_MYSQL { + table += " USE INDEX(idx_posts_channel_id_delete_at_create_at)" + } + query := s.getQueryBuilder(). Select("*"). - From("Posts"). + From(table). Where(sq.And{ sq.Gt{"CreateAt": time}, sq.Eq{"ChannelId": channelId},