Skip to content

[Bug]: ClickBench Q10 is significantly slower in MatrixOne than DuckDB #27672

Done

Description

@daviszhen

Parent issue

This is a sub-issue of #27665: significant gap in ClickBench query performance between MatrixOne and DuckDB.

Summary

ClickBench Q10 shows a significant MatrixOne performance gap against the DuckDB reference. The values below are elapsed seconds. Each result array is the three measurements emitted by the adapter: [run 1, run 2, run 3]; warm median is the median of the available run-2/run-3 values.

Query

SELECT RegionID, SUM(AdvEngineID), COUNT(*) AS c, AVG(ResolutionWidth), COUNT(DISTINCT UserID) FROM hits GROUP BY RegionID ORDER BY c DESC LIMIT 10;

Performance comparison

Engine Run 1 Run 2 Run 3 Warm median
MatrixOne 735.600 751.040 failed (null) 751.040
DuckDB 2.462 0.512 0.483 0.497
  • MatrixOne / DuckDB warm-median ratio: 1509.63x
  • Warm-median difference: 750.543 seconds
  • Raw MatrixOne timing array: [735.600, 751.040, null]
  • Raw DuckDB timing array: [2.462, 0.512, 0.483]

Environment and data

MatrixOne measurement:

  • Branch: main
  • Commit: 249a458ba567b26d1818fb0496b449135cb05ef2
  • Hardware: server 129
  • OS: CentOS
  • Dataset: ClickBench hits, 99,997,497 rows
  • MatrixOne raw output: /mnt/fastdata/ClickBench/matrixone/raw_results.md

DuckDB reference:

  • Result file: duckdb/results/20260511/c6a.metal.json
  • Machine label: c6a.metal
  • Result date: 2026-05-11

The two measurements were not collected on the same hardware. The DuckDB value is the latest local c6a.metal reference available; an apples-to-apples rerun on the same server is recommended before assigning a final performance regression magnitude.

Steps to reproduce

  1. Follow the reproduction steps in #27665, including the MatrixOne and DuckDB adapter READMEs:
  2. Use the same hits.tsv.gz dataset for both engines (the adapters support loading the gzip file directly).
  3. In the MatrixOne adapter, run ./benchmark.sh after setting MATRIXONE_HOME, MATRIXONE_DATA_FILE, MATRIXONE_HOST, MATRIXONE_PORT, MATRIXONE_USER, and MATRIXONE_PASSWORD as described in [Bug]: significant gap in clickbench query performance between matrixone and duckdb #27665.
  4. In the DuckDB adapter, run ./benchmark.sh after setting DUCKDB_BIN_PATH, DUCKDB_DB_PATH, and DUCKDB_DATA_PATH as described in the README.
  5. Compare the Q10 entry in queries.sql; run the query three times after loading the data and record elapsed time and whether the query completes successfully.

Notes

  • MatrixOne has one failed measurement (ERROR 2013 (HY000): Lost connection to MySQL server during query); the third value is shown as null.

Expected behavior

On comparable hardware with the same dataset, MatrixOne should not exhibit a large query-level performance gap for this workload. Please investigate the execution plan, operators, memory use, and any spill or connection/timeout behavior relevant to this query.

Activity

  1. added
    kind/bug
    on Aug 26, 2026
  2. XuPeng-SH commented on Aug 26, 2026

    @XuPeng-SH
    Contributor

    55 机器上的最小复现(2026-08-26)

    Q10 是 ClickBench 聚合查询,不是 TPC-H Q10:

    SELECT RegionID, SUM(AdvEngineID), COUNT(*) AS c,
           AVG(ResolutionWidth), COUNT(DISTINCT UserID)
    FROM hits
    GROUP BY RegionID
    ORDER BY c DESC
    LIMIT 10;

    为避免使用 1 亿行原始 ClickBench 数据,在 55(MatrixOne 8.0.30-MatrixOne-v4.0.0-rc4,SQL 端口 6001,单 CN 16 cores)建立了独立的最小投影表。Q10 实际只读取以下 4 列,列式扫描下这可以隔离聚合问题:

    CREATE DATABASE issue27672_q10_repro;
    CREATE TABLE issue27672_q10_repro.hits (
        RegionID INT NOT NULL,
        AdvEngineID SMALLINT NOT NULL,
        ResolutionWidth SMALLINT NOT NULL,
        UserID BIGINT NOT NULL
    );
    
    INSERT INTO issue27672_q10_repro.hits
    SELECT mod(result - 1, 100) + 1,
           mod(result, 8),
           800 + mod(result, 1200),
           result
    FROM generate_series(1, 1000000) g;

    数据特征:1,000,000 行、100 个 RegionID、1,000,000 个 UserID(每个 region 约 10,000 个 distinct user)。

    实测热查询耗时:

    • 原始 Q10:8.2–9.2 秒(连续运行)
    • 去掉 COUNT(DISTINCT UserID):约 0.013 秒
    • 只有 COUNT(DISTINCT UserID):约 0.02–0.03 秒
    • GROUP BY RegionID, COUNT(DISTINCT UserID):约 0.03 秒
    • GROUP BY RegionID, SUM(AdvEngineID), COUNT(DISTINCT UserID):约 8.2 秒

    EXPLAIN ANALYZE 将耗时定位到 Aggregate,而不是扫描、排序或返回结果:

    Aggregate: timeConsumed=16405ms inputRows=1000000 outputRows=100 MemorySize=36.34 MiB
    Table Scan: timeConsumed=4ms inputRows=1000000 ReadSize=4.23 MiB
    Sort/Project: timeConsumed=0ms
    

    对照计划显示,只有 DISTINCT 时会生成 GROUP BY RegionID, UserID 的两阶段聚合;混合普通聚合时则保留单个分组 Aggregate,导致每一行同时维护分组内 DISTINCT 状态。当前 optimizeDistinctAgg 对 AggList 长度大于 1 直接跳过改写,这与 Q10 的形状正好吻合。

    因此这个最小数据集已经稳定复现 issue 的慢路径;1 亿行的 751 秒是该路径在更大输入和更高 distinct 基数下的规模放大,不能仅归因于磁盘或连接超时。该结果是同机实测,合成数据不用于宣称与 ClickBench 绝对倍数相同。

  3. self-assigned this
    on Aug 26, 2026
  4. XuPeng-SH commented on Aug 27, 2026

    @XuPeng-SH
    Contributor

    Update: mixed aggregation slow path fixed by #27693

    #27693 (perf: parallelize high-cardinality mixed DISTINCT aggregation) is now merged. It addresses the root cause identified in the minimal reproduction: a mixed aggregate containing mergeable COUNT(DISTINCT ...) was costed only by the final GROUP BY cardinality. With a modest number of final groups, the planner did not distribute ownership of complete groups, leaving expensive exact DISTINCT state merges on the critical path.

    The fix preserves exact DISTINCT semantics and changes the topology only when the retained exact DISTINCT state is material relative to the input:

    • estimate the COUNT(DISTINCT ...) state cardinality in addition to final group cardinality;
    • for eligible, mergeable COUNT(DISTINCT) queries with enough active groups, shuffle by a group key so every logical group has one aggregate owner;
    • eliminate the downstream merge of that group's exact DISTINCT state;
    • retain the old topology for unsupported DISTINCT forms, inactive grouping-set keys, one physical owner, and cases without sufficient expected benefit.

    10 million-row isolated validation on 55

    The following is the same reduced Q10 shape used for diagnosis, scaled to 10,000,000 rows on the existing NVMe-backed isolated table: 100 balanced RegionID values and 10,000,000 unique UserID values.

    SELECT RegionID, SUM(AdvEngineID), COUNT(*) AS c,
           AVG(ResolutionWidth), COUNT(DISTINCT UserID)
    FROM issue27672_q10_repro.hits
    GROUP BY RegionID
    ORDER BY c DESC
    LIMIT 10;
    Revision Run 1 Run 2 Run 3 Mean
    Pre-#27693 baseline 98.792 s 101.389 s 102.119 s 100.77 s
    #27693 implementation 2.439 s 2.276 s 2.301 s 2.34 s

    This is a 43x reduction on the same data and machine. The execution plan uses a 16-core distributed aggregate topology; runtime sampling showed about 11–12 CPU cores in use during the query. The machine also had unrelated mo-service processes running, so this result is intentionally reported as a same-host before/after comparison rather than a clean-host absolute latency claim.

    The result confirms that the issue's observed multi-aggregate Q10 regression was caused by aggregate-state ownership/merge topology, rather than the scan, sort, client, or storage path. The prior 1 million-row diagnosis remains a smaller reproducer of the same root cause.

    Scope remaining after the fix

    This fix obtains parallelism from complete final-group ownership. It is therefore the correct fast path for many balanced groups, but cannot fully parallelize these shapes:

    SELECT COUNT(DISTINCT user_id) FROM events;            -- one final group
    
    SELECT region_id, COUNT(DISTINCT user_id)
    FROM events
    GROUP BY region_id;                                    -- few or skewed groups

    Those cases need partitioning by the canonical DISTINCT identity (group keys, DISTINCT arguments), not only by final group key. They also need a bounded-memory path when one group's exact state exceeds memory.

    This issue remains open until the original full ClickBench-scale workload has been re-run on the merged revision under a controlled, comparable environment and the remaining single-hot-group/memory boundaries have been separately covered.

  5. XuPeng-SH commented on Sep 1, 2026

    @XuPeng-SH
    Contributor

    2026-09-01 candidate diagnosis

    SQL shape: low-cardinality RegionID grouping with SUM/COUNT/AVG plus exact COUNT(DISTINCT UserID), followed by a tiny TopN.

    Confirmed historical cause / current-main status: #27693 was merged specifically for this issue after the recorded baseline. It addresses the planner ignoring the large retained DISTINCT state, the serial MergeGroup path, and expensive ordered-state growth/merge. The historical third run also lost the server connection, so 751s is a reliability signal as well as a stale performance number.

    Next action: re-run current main and require: exact results; three successful runs; post-query service health; no unexpected serial MergeGroup; balanced aggregate owners; bounded memory/spill; and DOP scaling. If a hot/few-group residual remains, route it to canonical (RegionID, UserID) partitioning in #27720.

    Do not optimize TopN first: all expensive work occurs before the ten RegionID results exist.

  6. assigned and unassigned on Nov 18, 2026
  7. XuPeng-SH commented on Nov 18, 2026

    @XuPeng-SH
    Contributor

    转 QA:ClickBench Q10 混合 DISTINCT 聚合修复

    @Ariznawlll 请接手验证。本 issue 保持 open;这是修复交接,不代表验收通过,也不代表原始 ClickBench 性能差距已全部消除。

    修复与证据

    • perf: parallelize high-cardinality mixed DISTINCT aggregation #27693 已于 2026-08-27 合入 main,merge SHA dfef35f7dd94de507d40dd18082b64e7f4b84e40。修复针对混合 SUM/COUNT/AVG 与高基数 COUNT(DISTINCT UserID) 的状态成本估计、分组所有权及串行 exact-state 合并慢路径。
    • 已有实测记录:55 机器上相同 10,000,000 行、100 个均衡 RegionID 的缩减 Q10,三次均值由 100.77 秒降为 2.34 秒,约 43 倍改善。机器同时有其他 mo-service,不能视为独占环境绝对性能,更不能等同于原始 99,997,497 行 ClickBench 或与 DuckDB 的同机比较。
    • PR 记录包含 owning-package UT/race、内存分配失败及单/多 owner 拓扑回归。早期 grouping-sets 反例在 19ee0241dd22453de010225225d937988d642648 修正,原 reviewer 后续 APPROVED 明确认定 blocker 已解决;旧 inline thread 的 unresolved 标记本身不构成未解决反例。
    • 上述是作者和 reviewer 的既有证据。本轮只做记录审查,未重新运行 SQL、性能测试或 CI,不声称当前 CI 全绿。

    待验证范围与边界

    1. 在明确记录二进制 SHA、数据和硬件的目标版本上重跑原始 Q10:精确结果一致、连续三次成功、查询后服务健康;原历史第三次断连接不能被忽略。
    2. 采集 EXPLAIN/实际执行拓扑、aggregate owner 分布、是否有意外串行 MergeGroup、DOP scaling、峰值内存和 spill。原规模性能尚未验收,不因尚未复测而将已合入修复继续留在开发交接阶段。
    3. 单组、少组、严重倾斜及超内存 exact DISTINCT 不由本修复完整解决,分别保留 [Enhancement]: Add adaptive partition-parallel execution for exact DISTINCT aggregates #27720 与 Support bounded distinct-key spill for high-NDV exact DISTINCT aggregates #27698 的独立跟进,不将这些能力宣称为已完成。无收益、非支持 DISTINCT、空 grouping set 和单物理 owner 应维持安全回退。
    4. 已核对元数据及完整讨论,另有已合入 4.2-dev 的 [4.2-dev] perf: backport COUNT(DISTINCT) optimizations (#27693, #28431) #32646(94dd363428668861bd0c8e60a197a115e950a1ce),其说明为 perf: parallelize high-cardinality mixed DISTINCT aggregation #27693 与 perf(aggexec): accelerate fixed-width COUNT(DISTINCT) #28431 的组合适配。请按实际待验分支分别记录版本,勿把 main 的历史结果直接等同于 4.2 验收;PR 作者报告真实三 CN 正常/race fixture;reviewer 在 a717091e1d8437911f8e90a17463d18ea668ee5a 上独立确认三 CN fixture 正常执行、arena/aggregate race 及 14 个新增根测试各 100 次 race。最终 head 85d603b1b23af625eb60a6d8db8c0e8ccb569749 的后续 review 记录为 interrupted,不能把早期 review 冒充最终 head 重验。局部 warm benchmark(fill 46.86→4.99 ms、merge 43.10→4.29 ms)不是完整 Q10 性能。4.2 不含 DISTINCT spill 或后续 main accounting,保留共享内存上限,无 per-query/tenant 隔离内存保证,升级要求全停。本次没有执行 backport,也不声称生产规模或升级验收已完成。

    写前已重新读取本 issue 的完整评论与时间线,未发现新增的修复后失败记录或当前必需的开发修复说明。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions