← Back to Pull requests

perf: MSQ Scan optimizations for limited queries with time order #20805

Closed
unknown closed this pull request without merging on Nov 09, 2026 19:19 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 18:31 UTC
Author

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 ReadableInputQueue as well as in the frame processor.

ReadableInputQueue

The 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

Benchmark                                                                         (clusteringCardinality)                          (layout)  (limit)  (maxNumTasks)  (numPartitionsPerTimeChunk)  (numTimeChunks)  (rowsPerSegment)  Mode  Cnt           Score         Error   Units
DartScanBenchmark.orderByTimeDescWithLimit                                                            100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3           3.527 ±      15.045   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3     8529842.787 ±  383319.341    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3          30.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.orderByTimeDescWithLimit                                                            100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3          15.022 ±      31.840   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3    29529043.992 ±  565655.109    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3       23453.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.orderByTimeDescWithLimit                                                            100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3          10.694 ±      19.818   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3     9496026.650 ±  554935.018    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3        2081.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.orderByTimeDescWithLimit                                                            100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3          33.965 ±      35.593   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3    45386236.769 ± 1168678.530    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3       56665.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.orderByTimeDescWithLimit                                                            100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3          18.702 ±      28.452   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3    60741227.241 ± 1209855.237    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3          30.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.orderByTimeDescWithLimit                                                            100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3          39.218 ±      19.398   ms/op
DartScanBenchmark.orderByTimeDescWithLimit:gc.alloc.rate.norm                                         100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3    87775161.737 ±  867135.325    B/op
DartScanBenchmark.orderByTimeDescWithLimit:rowsInAcquiredSegments                                     100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:rowsWritten                                                100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3       23453.000                     #
DartScanBenchmark.orderByTimeDescWithLimit:segmentsAcquired                                           100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3           4.128 ±      21.538   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3     8777996.164 ±  508307.635    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3          28.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3          18.781 ±      21.998   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3    24833779.962 ±  998937.700    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3     1400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3       11670.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3          14.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3           5.431 ±      22.003   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3     9221750.323 ±  549502.277    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3        1027.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3          13.541 ±      27.357   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3    24687709.911 ±  860267.655    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3     1400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3       11681.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3          14.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3           5.216 ±      25.371   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3     9428558.465 ± 1268395.876    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3      400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3          28.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3           4.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit                                             100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3          13.305 ±      16.271   ms/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:gc.alloc.rate.norm                          100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3    28366766.077 ±  816751.329    B/op
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsInAcquiredSegments                      100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3     1400000.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:rowsWritten                                 100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3       11670.000                     #
DartScanBenchmark.whereClusterKeyOrderByTimeDescWithLimit:segmentsAcquired                            100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3          14.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3         243.892 ±     135.524   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3   413554060.465 ± 4321832.966    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100                   V9_TIME_ORDERED       10              2                            4               30            100000  avgt    3         120.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3         243.943 ±      96.148   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3   413558658.159 ± 3912816.129    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100                   V9_TIME_ORDERED    10000              2                            4               30            100000  avgt    3         120.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3         277.485 ±     168.905   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3   415721710.222 ± 4320967.855    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100  V10_UNCLUSTERED_CLUSTERING_ORDER       10              2                            4               30            100000  avgt    3         120.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3         275.394 ±      59.500   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3   415770697.099 ± 4024462.218    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100  V10_UNCLUSTERED_CLUSTERING_ORDER    10000              2                            4               30            100000  avgt    3         120.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3         752.939 ±     454.061   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3  2279697331.429 ±  985166.164    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100                     V10_CLUSTERED       10              2                            4               30            100000  avgt    3         120.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit                                                      100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3         745.235 ±     259.674   ms/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:gc.alloc.rate.norm                                   100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3  2291610880.000 ± 1553056.464    B/op
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsInAcquiredSegments                               100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3    12000000.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:rowsWritten                                          100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3      267502.000                     #
DartScanBenchmark.whereDimOrderByTimeDescNoLimit:segmentsAcquired                                     100                     V10_CLUSTERED    10000              2                            4               30            100000  avgt    3         120.000                     #

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 (n is the limit parameter):

  • orderByTimeDescWithLimit: SELECT FROM scan_ds ORDER BY __time DESC LIMIT n

  • whereClusterKeyOrderByTimeDescWithLimit: SELECT FROM scan_ds WHERE clusterKey1 = '1' ORDER BY __time DESC LIMIT n

  • whereDimOrderByTimeDescNoLimit: SELECT FROM scan_ds WHERE dimSecondary = '7' ORDER BY __time DESC
  • Layouts (equivalent to TIME_ORDERED, UNCLUSTERED and CLUSTERED in SqlClusteredSegmentsBenchmark):

  • V9_TIME_ORDERED: V9 segment sorted by __time first.

  • 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:

  • Segments / Rows written: segments the scan stage acquired, and rows it wrote to its output, summed over workers. a → b is master → this branch, bold when it changes by 1% or more; a single value means no change.

  • Alloc / query: bytes allocated per query (gc.alloc.rate.norm), in MB (10^6 bytes).

  • ms/op: average time of one query (± 99.9% CI over 3 iterations), from submitting the SQL to collecting the query report, with the controller and workers in one JVM. Speedup is master / this branch and should be taken with a grain of salt. This is all local processing and on my laptop, etc. I'm more interested in the counters that explicitly show less work being done for optimized queries.
  • whereDimOrderByTimeDescNoLimit ignores the limit parameter, so its two rows per layout run the same query; it is the control, where nothing should change.

    <hr>

    Release note

    msq-dart and msq-task limited 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
    ReadableInputQueue
    ScanQueryFrameProcessor
    InputPruner
    ScanLimitTracker
    RowCountScanLimitTracker
    * 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:

  • been self-reviewed.

  • - [ ] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)
  • added documentation for new or modified features or behaviors.

  • a release note entry in the PR description.

  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added or updated version, license, or notice information in licenses.yaml

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

  • added integration tests.

  • been tested in a test Druid cluster.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: gianm Review duration: 14447m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository