diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index e6bc594cee7..4299754058b 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -1809,6 +1809,10 @@ func (r *bucketIndexReader) ExpandedPostings(ctx context.Context, ms []*labels.M keys = append(keys, allPostingsLabel) } + // Sorting will improve the performance dramatically if the dataset is relatively large + // since entries in the postings offset table was sorted by label name and value, + // the sequential reading is much faster. + sort.Sort(labels.Labels(keys)) fetchedPostings, err := r.fetchPostings(ctx, keys) if err != nil { return nil, errors.Wrap(err, "get postings") @@ -1890,7 +1894,6 @@ func checkNilPosting(l labels.Label, p index.Postings) index.Postings { func toPostingGroup(lvalsFn func(name string) ([]string, error), m *labels.Matcher) (*postingGroup, error) { if m.Type == labels.MatchRegexp { if vals := findSetMatches(m.Value); len(vals) > 0 { - sort.Strings(vals) toAdd := make([]labels.Label, 0, len(vals)) for _, val := range vals { toAdd = append(toAdd, labels.Label{Name: m.Name, Value: val})