diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 43d5e7392..073602323 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -2369,7 +2369,21 @@ func (tmm *tagToMetricIDsRowsMerger) flushPendingMetricIDs(dstData []byte, dstIt dstData = mp.Tag.Marshal(dstData) // Use sort.Sort instead of sort.Slice in order to reduce memory allocations sort.Sort(&tmm.pendingMetricIDs) - for _, metricID := range tmm.pendingMetricIDs { + pendingMetricIDs := tmm.pendingMetricIDs + if len(dstItems) == 0 { + // Put the first item with a single metricID, since this item goes into index, so it must be short. + dstData = encoding.MarshalUint64(dstData, pendingMetricIDs[0]) + dstItems = append(dstItems, dstData[dstDataLen:]) + pendingMetricIDs = pendingMetricIDs[1:] + if len(pendingMetricIDs) == 0 { + tmm.pendingMetricIDs = tmm.pendingMetricIDs[:0] + return dstData, dstItems + } + dstDataLen = len(dstData) + dstData = marshalCommonPrefix(dstData, nsPrefixTagToMetricIDs, mp.AccountID, mp.ProjectID) + dstData = mp.Tag.Marshal(dstData) + } + for _, metricID := range pendingMetricIDs { dstData = encoding.MarshalUint64(dstData, metricID) } tmm.pendingMetricIDs = tmm.pendingMetricIDs[:0]