perf: MSQ Scan optimizations for limited queries with time order #20805
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #20805"
general
weight: 0.35
gianm
Description
This PR introduces changes to MSQ that aim to improve the performance of limited scan queries that are time ordered. It approaches the problem both in the
ReadableInputQueueas well as in the frame processor.ReadableInputQueueThe queue formerly handed out inputs (realtime servers, frames, and segments in that preferred order). It focused on handing out whatever was ready to process over everything else. While this makes sense in many cases, it can lead to processing rows that never stood a chance to be included in the stage output in others.
My change to the queue adds an optional time ordering as well as an input pruner that can outright skip the processing of segments we know cannot contribute to the stage output. The new input pruner replaces, but does not change the behavior of, the existing mechanism in place that allowed the queue to skip segments during a limited scan with no ordering.
If the queue is told that there is time ordering, it sorts the loadable segments and forgoes sending any already cached segments to the front of the line for consumption. Note that non time ordered queries will still prioritize these already cached segments.
The new input pruner aims to avoid loading and handing out any segment who it knows won't contribute to stage output. It can also all-together stop queue processing if it knows that everything stage output needs has been processed (only exercised by limited scan with no ordering).
The upshot here is that we can now avoid known zero-value work on the processors.
Realtime data
the input queue's treatment of realtime queries does not change. We still eagerly fire the async query and when results are ready they will be processed with preference.
ScanLimitTracker
The scan limit tracker implements the input pruner logic needed by the readable input queue and also provides the mechanism for query processing to skip rows or even outright stop a cursor when it finds rows who cannot contribute to stage output.
##### RowCountScanLimitTracker
Contains the logic for the already existing optimizations for limited scans with no ordering. Uses a limit tracker shared across processors that allows them to stop their cursors early if the limit has been reached. The readable input queue sees the shared limit as well, allowing it to stop processing the queue early.
##### TimeOrderedScanLimitTracker
The tracker for optimized limited scans with time order as the first order by. This one is a bit more resource intensive since we need to track the timestamps of the current best k rows. In this implementation I have chosen to use a buffered top k with quick select to track the best row timestamps. There is a hard cap on the offset + limit of 1 million, any query with higher offset + limit will not use this optimization.
Along with the buffer of timestamps, we track the current worst row in the top k. we use that in multiple ways:
The readable input queue uses it to skip entire segments when it knows that the interval of a prospect segment could not include a row with a timestamp that beats the threshold.
The RowGate uses it to skip rows that don't beat it or outright stop the cursor if we know no future row in the cursor can beat it. We can only do the latter when we have a cursor in sorted order that matches our query time ordering.
###### commons-numbers for quick selection implementation
This does pull in a new dependency to take advantage of the pre-rolled quick select implementation
###### why not a heap?
My initial approach used a heap for the structure tracking the k best timestamps. I ended up abandoning that approach during early benchmarking that flagged poor performance due to excessive heap operations under a lock when segment time ordering worked against the query. such as a DESC query against a segment that was ordered by
dim1, dim2, __time. The buffered top k approach in this PR benchmarked much better.Benchmarks
see https://github.com/apache/druid/pull/20805#issuecomment-6534644834 for more data that is trying to take on a more realistic form. the base benchmarks were a bit of a best case scenario. I plan to re-write this part of the description with everything consolidated soon.
DartScanBenchmark: 30 one-day time chunks × 4 segments (120 segments × 100,000 rows = 12M rows),clusteringCardinality=100,maxNumTasks=2,-prof gc. Master and this branch are built on the same master commit.This branch
Simplified summary of the above table for readability
| Query | Layout | Limit | ms/op (± 99.9% CI) | Alloc / query | Segments | Rows in segments | Rows written |
|---|---|---:|---:|---:|---:|---:|---:|
|
orderByTimeDescWithLimit| V9_TIME_ORDERED | 10 | 3.5 ± 15.0 | 8.5 MB | 4 | 400,000 | 30 || | V9_TIME_ORDERED | 10000 | 15.0 ± 31.8 | 29.5 MB | 4 | 400,000 | 23,453 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10 | 10.7 ± 19.8 | 9.5 MB | 4 | 400,000 | 2,081 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10000 | 34.0 ± 35.6 | 45.4 MB | 4 | 400,000 | 56,665 |
| | V10_CLUSTERED | 10 | 18.7 ± 28.5 | 60.7 MB | 4 | 400,000 | 30 |
| | V10_CLUSTERED | 10000 | 39.2 ± 19.4 | 87.8 MB | 4 | 400,000 | 23,453 |
|
whereClusterKeyOrderByTimeDescWithLimit| V9_TIME_ORDERED | 10 | 4.1 ± 21.5 | 8.8 MB | 4 | 400,000 | 28 || | V9_TIME_ORDERED | 10000 | 18.8 ± 22.0 | 24.8 MB | 14 | 1,400,000 | 11,670 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10 | 5.4 ± 22.0 | 9.2 MB | 4 | 400,000 | 1,027 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10000 | 13.5 ± 27.4 | 24.7 MB | 14 | 1,400,000 | 11,681 |
| | V10_CLUSTERED | 10 | 5.2 ± 25.4 | 9.4 MB | 4 | 400,000 | 28 |
| | V10_CLUSTERED | 10000 | 13.3 ± 16.3 | 28.4 MB | 14 | 1,400,000 | 11,670 |
|
whereDimOrderByTimeDescNoLimit| V9_TIME_ORDERED | none | 243.9 ± 135.5 | 413.6 MB | 120 | 12,000,000 | 267,502 || | V9_TIME_ORDERED | none | 243.9 ± 96.1 | 413.6 MB | 120 | 12,000,000 | 267,502 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | none | 277.5 ± 168.9 | 415.7 MB | 120 | 12,000,000 | 267,502 |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | none | 275.4 ± 59.5 | 415.8 MB | 120 | 12,000,000 | 267,502 |
| | V10_CLUSTERED | none | 752.9 ± 454.1 | 2,279.7 MB | 120 | 12,000,000 | 267,502 |
| | V10_CLUSTERED | none | 745.2 ± 259.7 | 2,291.6 MB | 120 | 12,000,000 | 267,502 |
Per-side tables: Rows in segments is the total rows in the acquired segments; the other columns are described under the before / after table.
Before (master) / after (my change)
| Query | Layout | Limit | Segments | Rows written | Alloc / query (MB) | ms/op master | ms/op branch | "Speedup" |
|---|---|---:|---:|---:|---:|---:|---:|---:|
|
orderByTimeDescWithLimit| V9_TIME_ORDERED | 10 | 120 → 4 | 1,200 → 30 | 17.5 → 8.5 | 11.8 ± 12.5 | 3.5 ± 15.0 | 3.3× || | V9_TIME_ORDERED | 10000 | 120 → 4 | 1,200,000 → 23,453 | 513.2 → 29.5 | 254.1 ± 64.2 | 15.0 ± 31.8 | 16.9× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10 | 120 → 4 | 12,000,000 → 2,081 | 5,746.5 → 9.5 | 3,443.2 ± 725.9 | 10.7 ± 19.8 | 322.0× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10000 | 120 → 4 | 12,000,000 → 56,665 | 5,809.5 → 45.4 | 3,701.4 ± 480.1 | 34.0 ± 35.6 | 109.0× |
| | V10_CLUSTERED | 10 | 120 → 4 | 1,200 → 30 | 1,574.9 → 60.7 | 396.7 ± 231.5 | 18.7 ± 28.5 | 21.2× |
| | V10_CLUSTERED | 10000 | 120 → 4 | 1,200,000 → 23,453 | 2,443.2 → 87.8 | 1,046.2 ± 86.3 | 39.2 ± 19.4 | 26.7× |
|
whereClusterKeyOrderByTimeDescWithLimit| V9_TIME_ORDERED | 10 | 120 → 4 | 1,200 → 28 | 18.2 → 8.8 | 12.4 ± 17.6 | 4.1 ± 21.5 | 3.0× || | V9_TIME_ORDERED | 10000 | 120 → 14 | 121,035 → 11,670 | 91.2 → 24.8 | 113.6 ± 50.8 | 18.8 ± 22.0 | 6.0× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10 | 120 → 4 | 121,035 → 1,027 | 67.3 → 9.2 | 42.3 ± 27.0 | 5.4 ± 22.0 | 7.8× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | 10000 | 120 → 14 | 121,035 → 11,681 | 90.4 → 24.7 | 60.2 ± 34.8 | 13.5 ± 27.4 | 4.4× |
| | V10_CLUSTERED | 10 | 120 → 4 | 1,200 → 28 | 35.8 → 9.4 | 17.5 ± 21.6 | 5.2 ± 25.4 | 3.3× |
| | V10_CLUSTERED | 10000 | 120 → 14 | 121,035 → 11,670 | 128.2 → 28.4 | 60.5 ± 44.3 | 13.3 ± 16.3 | 4.5× |
|
whereDimOrderByTimeDescNoLimit| V9_TIME_ORDERED | none | 120 | 267,502 | 413.5 → 413.6 | 231.7 ± 60.1 | 243.9 ± 135.5 | 1.0× || | V9_TIME_ORDERED | none | 120 | 267,502 | 413.5 → 413.6 | 227.1 ± 37.3 | 243.9 ± 96.1 | 0.9× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | none | 120 | 267,502 | 415.7 | 280.5 ± 49.7 | 277.5 ± 168.9 | 1.0× |
| | V10_UNCLUSTERED_CLUSTERING_ORDER | none | 120 | 267,502 | 415.8 | 273.9 ± 79.6 | 275.4 ± 59.5 | 1.0× |
| | V10_CLUSTERED | none | 120 | 267,502 | 2,280.3 → 2,279.7 | 746.2 ± 448.8 | 752.9 ± 454.1 | 1.0× |
| | V10_CLUSTERED | none | 120 | 267,502 | 2,291.0 → 2,291.6 | 762.9 ± 347.9 | 745.2 ± 259.7 | 1.0× |
Queries (
nis thelimitparameter):orderByTimeDescWithLimit:SELECT FROM scan_ds ORDER BY __time DESC LIMIT nwhereClusterKeyOrderByTimeDescWithLimit:SELECT FROM scan_ds WHERE clusterKey1 = '1' ORDER BY __time DESC LIMIT nwhereDimOrderByTimeDescNoLimit:SELECT FROM scan_ds WHERE dimSecondary = '7' ORDER BY __time DESCLayouts (equivalent to
TIME_ORDERED,UNCLUSTEREDandCLUSTEREDinSqlClusteredSegmentsBenchmark):V9_TIME_ORDERED: V9 segment sorted by__timefirst.V10_UNCLUSTERED_CLUSTERING_ORDER: V10 segment sorted by clusterKey1, clusterKey2, then__time; not time-ordered.V10_CLUSTERED: V10 clustered base table, one value group per (clusterKey1, clusterKey2), each sorted by__time.Columns:
a → bis master → this branch, bold when it changes by 1% or more; a single value means no change.gc.alloc.rate.norm), in MB (10^6 bytes).whereDimOrderByTimeDescNoLimitignores thelimitparameter, so its two rows per layout run the same query; it is the control, where nothing should change.<hr>
Release note
msq-dartandmsq-tasklimited scan queries with a time ordering as the first order by can now run more efficiently by skipping processing of entire segments that will not contribute to stage output as well as skipping row processing and/or stopping processing mid-flight if no further rows can contribute to stage output.<hr>
##### Key changed/added classes in this PR
ReadableInputQueueScanQueryFrameProcessorInputPrunerScanLimitTrackerRowCountScanLimitTracker*
TimeOrderedScanLimitTracker<hr>
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
This PR has:
- [ ] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)