924060929 opened a new pull request, #70999:
URL: https://github.com/apache/doris/pull/70999

   ### Background
   
   Sampled `ANALYZE TABLE` and `ANALYZE TABLE ... WITH HISTOGRAM` use 
`FIXED_BLOCK` physical row-id block sampling on `DUP_KEYS` and `UNIQUE_KEYS` 
(MOW) tables, and `KEY_RANGE_BLOCK` merged sort-key interval sampling on 
hash-distributed `UNIQUE_KEYS` (MOR) and `AGG_KEYS` tables. On 
`RANDOM`-distributed `AGG_KEYS` tables, partial rows of the same key scatter 
across sibling buckets of a partition, so sampling previously pruned whole 
partitions and evaluated an `xxhash_64` key-tuple filter across all rows of 
selected partitions.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #70998 (depends on)
   PR Type: Performance
   
   Problem Summary:
   
   Two single-table bottlenecks remained for large-table sampled statistics: 
single-partition or wide-partition `RANDOM` `AGG_KEYS` tables scanned every 
physical row of selected partitions to evaluate `xxhash_64`, and uncompacted 
composite-key MOR/`AGG_KEYS` tablets with many overlapping segments repeated 
full-window binary searches across compressed pages when `enablePageCache = 
false`. Decomposing `KeyRangeBlockSampler` into interval-sampling and 
interval-location primitives enables coordinated partition-wide 
`KEY_RANGE_BLOCK` intervals across `RANDOM` `AGG_KEYS` buckets, bounded 
overlapping-segment subdivision, composite `KeyBoundsPB::max_key` pruning, and 
monotonic galloping `SegmentKeySeeker::lower_bound` seeks without storage 
format changes. Rolling upgrades fall back to `xxhash_64` until backends 
advertise `supports_shared_key_range_block_sampling_v1`.
   
   ### Details
   
   - **Change map:** 32 files (+2,035/-210): production code +1,469/-201 across 
25 files (`be/` +930, `fe/` +489, `gensrc/` +50), tests +566/-9 across 7 files:
     1. BE composite sort-key encoding, composite `KeyBoundsPB::max_key` 
pruning, and galloping `lower_bound` (`row_key_encoder.*`, 
`segment_key_seeker.*`, ~175 lines).
     2. BE `KeyRangeBlockSampler` interval split (`sample_intervals`, 
`locate_intervals`, `collect_weighted_cuts`), span coalescing, and 16-head 
stratified subdivision (`key_range_block_sampler.*`, `olap_scan_operator.cpp`, 
~625 lines).
     3. BE `PBackendService::sample_tablet_key_ranges` RPC and 
`supports_shared_key_range_block_sampling_v1` heartbeat flag 
(`internal_service.*`, `heartbeat_server.cpp`, `gensrc/`, ~180 lines).
     4. FE partition-wide `RANDOM` `AGG_KEYS` coordination and multi-BE 
weighted boundary merging (`RandomAggTabletSamplingMethod`, 
`FixedBlockSampleSpec`, `OlapScanNode`, ~690 lines).
   - **Why it was slow:** `RANDOM` `AGG_KEYS` SQL `xxhash_64` filtering scanned 
all physical rows in selected partitions (`100M` `ScanRows` across 5 columns on 
an unpartitioned 20M-row table). On uncompacted composite-key MOR/`AGG_KEYS` 
tablets, `max_key` pruning was single-column only, subdivision opened iterators 
across all overlapping segments, and binary search ping-ponged across 
compressed pages with `enablePageCache = false`.
   - **Optimization:** Encode composite sort keys into order-preserving byte 
strings (`RowKeyEncoder::encode_sort_key_fields`), prune segments via composite 
`max_key`, cap subdivision at 16 weight-stratified spans, gallop forward from 
the monotonic cursor in `SegmentKeySeeker::lower_bound`, and coordinate 
partition-wide `KEY_RANGE_BLOCK` intervals across `RANDOM` `AGG_KEYS` tablets 
via `sample_tablet_key_ranges`.
   - **Results:**
     - 20M-row unpartitioned `RANDOM BUCKETS 8` `AGG_KEYS` table (5M keys, 5 
columns): `ScanRows` drops 49.4x (`100M` -> `2.03M`), `ScanBytes` drops 5.5x 
(`1.70 GB` -> `306.7 MB`), `SAMPLE ROWS 400000` is 3.28x faster than full 
analyze (`2.814s` -> `0.858s`, 1.28% `row_count` error), and sampled `WITH 
HISTOGRAM` is 5.72x faster (`2.218s` -> `0.388s`).
     - 8-partition 16M-row `RANDOM BUCKETS 4` `AGG_KEYS` table: `ScanRows` 
drops 40.3x (`80M` -> `1.99M`) and latency drops 2.60x (`2.511s` -> `0.967s`, 
0.74% error).
     - 50-rowset overlapping composite `(VARCHAR, BIGINT)` `AGG_KEYS` table 
(`30M` rows -> `1.32M` keys): `ScanRows` drops 143x (`150M` -> `1.05M`, 1.42% 
error); galloping `lower_bound` cuts `enablePageCache = false` seek latency 
3.46x (`7.763s` -> `2.244s`), and 1,000-segment GTest sampling finishes in `10 
ms`.
   - **Applicability and cost:** Active on MOR and hash/random `AGG_KEYS` 
sampled statistics and histograms once candidate backends report 
`supports_shared_key_range_block_sampling_v1`, adding at most one RPC per 
sampled `RANDOM` partition per hosting backend (capped at `4,096` cuts).
   - **Behavior change:** `RANDOM`-distributed `AGG_KEYS` sampled statistics 
and histograms use shared `KEY_RANGE_BLOCK` instead of `xxhash_64` 
full-partition scans when backends support v1.
   
   ### Testing
   
   - Ran 19 BE unit tests (`FixedRowIdBlockSamplerTest`, 
`SegmentKeySeekerTest`, `KeyRangeBlockSamplerTest`, 
`VerticalSegmentWriterWritePathsTest.KeyRangeSeeksStoredKeys`) and FE unit 
tests (`FixedBlockTableSamplePlanTest`, `KeyRangeBlockSamplingTest`, 
`BackendHeartbeatHandlerTest`, `OlapAnalysisTaskTest`, `HistogramTaskTest`) 
locally — all passed.
   - Ran `test_sample_minmax_batch`, `test_fixed_block_sample`, 
`test_key_range_block_sample`, and `test_fixed_block_analyze` on a local FE+BE 
cluster (all passed) and benchmarked 16M-30M row `RANDOM` and 50-rowset 
overlapping `AGG_KEYS` tables.
   
   ### Notes for Reviewers
   
   - Stacked on #70998; review top commit `7b3e54b2373` for the standalone diff 
until #70998 merges.
   
   ### Release note
   
   Optimize `KEY_RANGE_BLOCK` statistics sampling for high-overlap uncompacted 
MOR/`AGG_KEYS` tablets and extend shared-interval `KEY_RANGE_BLOCK` sampling to 
`RANDOM`-distributed `AGG_KEYS` tables without storage format changes.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: No
   - Pre-submission status: Ready
   
   ### AI Disclosure
   
   - Tool: Antigravity
   - Generated: code, tests, this description
   - Human review: not stated
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to