Skip to content

[Performance]: untyped NULL prepared parameter formats every DECIMAL row #26845

Closed

Description

@XuPeng-SH

Is there an existing issue for performance?

  • I have checked the existing issues.

Searched open and closed issues for DECIMAL, NULL prepared parameters, binary protocol, decimal128ToFloat, and Decimal128.Format. No matching report was found.

Environment

  • Version or commit-id: main / 08770e1beeb23239c91165492f2fe85243a3c66a
  • Hardware parameters: single-node launch, CN/TN memory cache 6 GB
  • OS type: Linux
  • Others: MySQL protocol port 6001; go-sql-driver/mysql with interpolateParams=false (binary COM_STMT_PREPARE/EXECUTE); non-partitioned table

Details of Performance

A reused binary-protocol prepared statement with an untyped NULL parameter against DECIMAL(20,4) is about 5.5x slower in single-query p50 and 16.3x lower throughput than the same statement with an explicit DECIMAL cast.

All variants return the same correct 9,792 rows from a 50,000-row table:

literal:                    k_dec <=> NULL
prepared untyped:           k_dec <=> ?                       arg=nil
prepared typed workaround:  k_dec <=> CAST(? AS DECIMAL(20,4)) arg=nil

baseline p50, 9 interleaved repeats:
literal                     4.485 ms
prepared untyped           24.481 ms  (reused statement)
prepared typed              4.657 ms  (reused statement)

8 workers, 10 seconds:
prepared untyped              282.48 QPS
prepared typed               4617.55 QPS

BIGINT and VARCHAR <=> ? NULL controls do not show this gap. Non-NULL DECIMAL equality/range prepared controls are also close to literal performance.

Additional information

Reproduction schema/data:

CREATE DATABASE mo_binary_protocol_diff;
CREATE TABLE mo_binary_protocol_diff.protocol_probe (
  id BIGINT PRIMARY KEY,
  k_dec DECIMAL(20,4) NULL
);
INSERT INTO mo_binary_protocol_diff.protocol_probe
SELECT result,
       CASE WHEN result%7=0 THEN NULL
            ELSE CAST((result%100000)/10.0 AS DECIMAL(20,4)) END
FROM generate_series(1,50000) g;

Prepare and repeatedly execute these through MySQL binary protocol with a NULL parameter:

-- Slow
SELECT COUNT(*) FROM mo_binary_protocol_diff.protocol_probe FORCE INDEX(PRIMARY)
WHERE k_dec <=> ?;

-- Fast workaround
SELECT COUNT(*) FROM mo_binary_protocol_diff.protocol_probe FORCE INDEX(PRIMARY)
WHERE k_dec <=> CAST(? AS DECIMAL(20,4));

CPU profile for the untyped path (8 workers, 20 seconds, 165.27 CPU-seconds sampled):

92.10% cum  function.decimal128ToFloat
83.93% cum  types.Decimal128.Format
33.69% cum  fmt.Sprintf
26.39% cum  runtime.concatstrings
11.77% flat types.Decimal128.Div128

The typed path instead uses the fixed DECIMAL null-safe operator (opBinaryFixedFixedToFixedNullSafe) and does not format every DECIMAL value as text. This indicates the untyped NULL parameter causes a per-row DECIMAL-to-float conversion path even though the comparison result only depends on NULL semantics.

Artifacts generated by the harness:

  • round33_binary_protocol_differential.go
  • round33_decimal_null_profile.go
  • round33_decimal_null_untyped_cpu.pb.gz
  • round33_decimal_null_typed_cpu.pb.gz

Activity

  1. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Expanded type matrix on the same baseline confirms this is DECIMAL-family-specific, not Decimal128-only or a generic fixed-type prepared-parameter cost:

    type / reused prepared p50
    BIGINT              5.394 ms
    DECIMAL(10,2)      19.395 ms
    DECIMAL(20,4)      24.622 ms
    DOUBLE              5.985 ms
    DATE                3.694 ms
    VARCHAR             4.035 ms
    

    All literal/binary result sets are identical. Both Decimal64 and Decimal128 take the slow untyped-NULL conversion path; DOUBLE/DATE/BIGINT/VARCHAR are controls. Explicit CAST(? AS DECIMAL(20,4)) remains fast at 4.893 ms.

  2. added
    kind/bug
    severity/s0
    on Aug 9, 2026
  3. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Draft PR #26849 independently closes the performance reproduction on 10.222.1.55: reused untyped NULL p50 4.370ms vs typed 5.860ms (baseline 24.481/4.657ms); 8 workers 10s untyped 4936.02 QPS vs typed 4980.56 QPS, both 0 failures (baseline 282.48/4617.55 QPS). The pathological Decimal128.Format path is no longer observable through workload latency/throughput.

  4. added a commit that references this issue on Aug 9, 2026
    e09f46e
  5. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:8be242b25bf9a44d73a3b1cc1db75fd0264a293c
    • 部署方式:本机单节点 Proxy/CN/TN,MySQL 协议端口 6001;Go go-sql-driver/mysql,显式 interpolateParams=false
    • 测试日期:2026-08-10

    原问题验证

    在 50,000 行 DECIMAL(20,4) 表中每第 7 行为 NULL,使用真实 COM_STMT_PREPARE/COM_STMT_EXECUTE 复跑 12 次:d <=> ? 绑定 NULL、d <=> CAST(? AS DECIMAL(20,4)) 绑定 NULL、以及 d <=> NULL literal control 的计数均稳定为 7142。直接/typed 的单次耗时比值位于 0.80–1.06,未出现原问题关注的数量级放大;同一物理 session 持续可用。

    Happy Path 覆盖

    1. 12 轮直接二进制 NULL DECIMAL 绑定都返回 7142。
    2. 12 轮显式 DECIMAL typed control 都返回 7142,与 literal NULL control 一致。
    3. 场景固定为单物理连接,确认不是连接切换掩盖参数元数据/缓存问题。
    4. 同一脚本还覆盖 [Bug]: DECIMAL <=> prepared parameter loses precision through DOUBLE cast #26847 的高精度 non-NULL DECIMAL,以确保二进制绑定分别经过 NULL/non-NULL 两条关键路径。

    Unhappy Path 覆盖

    不适用:NULL 是本 issue 的合法输入,不是应被拒绝的异常路径;查询为只读,没有失败原子性契约。已在每轮后保留 session health query,确认重复执行没有把连接置为不可用状态。

    白盒验证

    确认修复 e09f46e5b(fix prepared DECIMAL comparison coercion)已经在当前 main。该提交说明直接 prepared marker 原先按 TEXT 走泛型 string/numeric coercion;本评论不把本机因 usearch.h/xxhash.h/roaring.h 缺失而未完成的 planner 包编译计为白盒通过。

    回归测试

    已加入 motr 草稿 PR #89:

    • script/14_issue_regression/issue_26845_26847_protocol_decimal.go
    • 固定输出:script/14_issue_regression/out/issue_26845_26847_protocol_decimal.out
    • suite 跳转:suites/14_issue_regression/t/issue_26845_26847_protocol_decimal.test

    性能时间没有写入 golden,避免把机器噪声当作阈值;该回归稳定断言实际二进制参数语义、计数和 session 可用性。若要重新建立性能门槛,应放专门 perf 环境而非 motr。

    测试证据

    脚本自身连续 3 轮输出固定 PASS;motr 精确选择集合连续 3 轮 2/2 success、43 条语句成功。文件和输出可在 PR #89 复核。

    测试结论

    测试通过。

  6. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:3c7392a0f86b23712da71f31d7a9198a2c8591a9
    • 部署方式:本地单节点 Log/TN/CN;go-sql-driver/mysql 二进制 prepared 协议,50,000 行 DECIMAL(20,4) 数据;测试日期:2026-08-10。

    原问题验证

    d <=> ?(NULL 参数)与 d <=> CAST(? AS DECIMAL(20,4)) 均返回 7,142 行。连续性能采样的 p50 untyped/typed 比为 0.99、0.98,吞吐比为 1.23、1.26,未观察到 issue 中的数量级退化。

    Happy Path 覆盖

    1. 50,000 行、NULL 密集 DECIMAL 表上,untyped、显式 typed 和 literal NULL 语义一致。
    2. 每轮分别执行 untyped/typed 12 次;固定 4 种索引布局、精确高精度 DECIMAL、相邻不存在值和 NULL 的 statement rebind 也已覆盖。

    Unhappy Path 覆盖

    1. 不存在的相邻高精度 DECIMAL 参数返回空集;NULL 参数只命中 NULL 行,未出现错行或隐式类型污染。
    2. 所有二进制协议查询完成后会话仍可用。性能问题没有可移植的固定毫秒阈值,MOTR 仅固化结果正确性和稳定性,不以时延阈值制造不稳定 CI。

    白盒验证

    不适用:本次为协议层黑盒性能/正确性验证。

    回归测试

    已存在并复跑 motr PR #89 的 script/14_issue_regression/issue_26845_26847_protocol_decimal.go(PR 当前 commit a6a50dfcf7053027dab5e9117fde7bdd7b5cdefb)。

    测试证据

    MOTR 连续 3 轮:该 scenario 与关联场景合计 15/15 成功、171 条断言成功;性能采样结果见原问题验证。

    测试结论

    测试通过。

  7. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    Collaborator

    需要加大数据量测试,先不关闭

  8. Ariznawlll commented on Aug 19, 2026

    @Ariznawlll
    Collaborator

    Big-data 回归补充

    已补充 #26845 的 big-data 回归:

    • mo-nightly-regression#1374 增加真实 binary prepared driver:script/issue_26845_prepared_decimal_null_bigdata.py。
    • mo-nightly-regression#1375 将该 driver 接入 query_tests_1y,作为 main-only matrix case 执行;4.2-dev 跳过。
    • 覆盖 100 万行 DECIMAL(20,4)、每 7 行一个 NULL、literal/untyped NULL/typed NULL 精确计数,以及 8-client p50/QPS 对照。
  9. added 3 commits that reference this issue on Sep 17, 2026
    aa75c99
    4e14d35
    8521386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugkind/performanceseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions