Skip to content

LIMIT BY on a stream already sorted by its keys is about 2x slower (26.6 -> 26.8) #123700

Closed

Description

@fm4v

Caused by: #106502

Description

ORDER BY k1, k2, ver DESC LIMIT 1 BY k1, k2 over a ReplacingMergeTree with String keys is about 2x slower on 26.8 than on 26.6, and about 4.8x slower on a single merged part. The single-threaded LimitBySortedStreamTransform spends about 4x more time on the same input rows.

Regression between 26.6.1.2301 (OLD) and 26.8.1.390 (NEW).

How to reproduce

Run the setup once, then measure only the SELECT on each version.

CREATE TABLE default.t1 (c1 String, c2 String, c5 UInt64) ENGINE = SharedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}', c5) ORDER BY (c1, c2);
INSERT INTO default.t1 SELECT concat('UC', substring(hex(sipHash64(number % 25000)), 1, 22)) AS c1, substring(hex(sipHash64(number, 1)), 1, 11) AS c2, 1 AS c5 FROM numbers(12000000);
INSERT INTO default.t1 SELECT concat('UC', substring(hex(sipHash64(number % 25000)), 1, 22)) AS c1, substring(hex(sipHash64(number, 1)), 1, 11) AS c2, 2 AS c5 FROM numbers(8000000);
OPTIMIZE TABLE default.t1 FINAL;
SELECT count() FROM (SELECT c1, c2, c5 FROM default.t1 ORDER BY c1 ASC, c2 ASC, c5 DESC LIMIT 1 BY c1, c2)
SETTINGS compatibility = '25.8', max_threads = '8';

Performance

Original query (milliseconds; SQL omitted):

OLD 26.6.1.2301 NEW 26.8.1.390 NEW/OLD
12078 22657 1.88×

Synthetic repro above (milliseconds):

Query OLD 26.6.1.2301 NEW 26.8.1.390 NEW/OLD
Query 439 2137 4.87×

Expected behavior

The LIMIT BY step on a stream already sorted by the LIMIT BY keys should cost about the same per row as in 26.6. The result is unchanged (identical counts on both versions).

Version info

Activity

  1. added
    bug
    replay-finding
    on Oct 3, 2026
  2. added
    comp-query-execution
    on Oct 3, 2026
  3. removed
    bug
    on Oct 3, 2026
  4. fm4v commented on Oct 3, 2026

    @fm4v
    MemberAuthor

    @groeneai Please investigate the root cause of this regression and create a fix.

  5. groeneai commented on Oct 3, 2026

    @groeneai
    Collaborator

    Acknowledged. I'm investigating the LimitBySortedStreamTransform regression on sorted String keys; I'll follow up here with a PR (or my findings) shortly.

  6. groeneai commented on Oct 4, 2026

    @groeneai
    Collaborator

    Fix: #123808. Since #106502, each group's search restarted on the leading key column, so on long leading-key runs it rescanned the rest of that run for every group (18.9 String comparisons per row here, against 2 before). The fix remembers the run of each key prefix within a chunk, and the issue query goes from 1.08 s to 0.31 s with identical results; negative LIMIT BY and the full_sorting_merge join had the same cost and are fixed too.

  7. added 2 commits that reference this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-query-executionperformancereplay-finding

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions