v.22.8Performance Improvement

Distinct Optimization and Memory Efficiency in ClickHouse with Sorted Input Handling

DISTINCT in order with ORDER BY: Deduce way to sort based on input stream sort description. Skip sorting if input stream is already sorted. #38719 (Igor Nikonov). Improve memory usage (significantly) and query execution time + use DistinctSortedChunkTransform for final distinct when DISTINCT columns match ORDER BY columns, but rename to DistinctSortedStreamTransform in EXPLAIN PIPELINE → this improves memory usage significantly + remove unnecessary allocations in hot loop in DistinctSortedChunkTransform. #39432 (Igor Nikonov). Use DistinctSortedTransform only when sort description is applicable to DISTINCT columns, otherwise fall back to ordinary DISTINCT implementation + it allows making less checks during DistinctSortedTransform execution. #39528 (Igor Nikonov). Fix: DistinctSortedTransform didn't take advantage of sorting. It never cleared HashSet since clearing_columns were detected incorrectly (always empty). So, it basically worked as ordinary DISTINCT (DistinctTransform). The fix reduces memory usage significantly. #39538 (Igor Nikonov).