Skip to content

Commit da119b0

Browse files
authored
Speedup time_series agg by caching current tsid ordinal, parent bucket ordinal and buck ordinal (#91784)
This avoids needlessly adding the same parent bucket ordinal or TSIDs to `BytesKeyedBucketOrds`. Relates to #74660
1 parent 150462c commit da119b0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/timeseries/TimeSeriesAggregator.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,34 @@ protected void doClose() {
9292
protected LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx, LeafBucketCollector sub) throws IOException {
9393
return new LeafBucketCollectorBase(sub, null) {
9494

95+
// Keeping track of these fields helps to reduce time spent attempting to add bucket + tsid combos that already were added.
96+
long currentTsidOrd = -1;
97+
long currentBucket = -1;
98+
long currentBucketOrdinal;
99+
95100
@Override
96101
public void collect(int doc, long bucket) throws IOException {
102+
// Naively comparing bucket against currentBucket and tsid ord to currentBucket can work really well.
103+
// TimeSeriesIndexSearcher ensures that docs are emitted in tsid and timestamp order, so if tsid ordinal
104+
// changes to what is stored in currentTsidOrd then that ordinal well never occur again. Same applies
105+
// currentBucket if there is no parent aggregation or the immediate parent aggregation creates buckets
106+
// based on @timestamp field or dimension fields (fields that make up the tsid).
107+
if (currentBucket == bucket && currentTsidOrd == aggCtx.getTsidOrd()) {
108+
collectExistingBucket(sub, doc, currentBucketOrdinal);
109+
return;
110+
}
111+
97112
long bucketOrdinal = bucketOrds.add(bucket, aggCtx.getTsid());
98113
if (bucketOrdinal < 0) { // already seen
99114
bucketOrdinal = -1 - bucketOrdinal;
100115
collectExistingBucket(sub, doc, bucketOrdinal);
101116
} else {
102117
collectBucket(sub, doc, bucketOrdinal);
103118
}
119+
120+
currentBucketOrdinal = bucketOrdinal;
121+
currentTsidOrd = aggCtx.getTsidOrd();
122+
currentBucket = bucket;
104123
}
105124
};
106125
}

0 commit comments

Comments
 (0)