Repository navigation
Description
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(binaryCOM_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.goround33_decimal_null_profile.goround33_decimal_null_untyped_cpu.pb.gzround33_decimal_null_typed_cpu.pb.gz
Activity
XuPeng-SH commentedon Aug 9, 2026
Expanded type matrix on the same baseline confirms this is DECIMAL-family-specific, not Decimal128-only or a generic fixed-type prepared-parameter cost:
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.XuPeng-SH commentedon Aug 9, 2026
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.
fix prepared DECIMAL comparison coercion (#26849)
Ariznawlll commentedon Aug 10, 2026
测试版本
main8be242b25bf9a44d73a3b1cc1db75fd0264a293c6001;Gogo-sql-driver/mysql,显式interpolateParams=false原问题验证
在 50,000 行
DECIMAL(20,4)表中每第 7 行为 NULL,使用真实COM_STMT_PREPARE/COM_STMT_EXECUTE复跑 12 次:d <=> ?绑定 NULL、d <=> CAST(? AS DECIMAL(20,4))绑定 NULL、以及d <=> NULLliteral control 的计数均稳定为7142。直接/typed 的单次耗时比值位于0.80–1.06,未出现原问题关注的数量级放大;同一物理 session 持续可用。Happy Path 覆盖
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.goscript/14_issue_regression/out/issue_26845_26847_protocol_decimal.outsuites/14_issue_regression/t/issue_26845_26847_protocol_decimal.test性能时间没有写入 golden,避免把机器噪声当作阈值;该回归稳定断言实际二进制参数语义、计数和 session 可用性。若要重新建立性能门槛,应放专门 perf 环境而非 motr。
测试证据
脚本自身连续 3 轮输出固定 PASS;motr 精确选择集合连续 3 轮
2/2 success、43 条语句成功。文件和输出可在 PR #89 复核。测试结论
测试通过。
Ariznawlll commentedon Aug 10, 2026
测试版本
main3c7392a0f86b23712da71f31d7a9198a2c8591a9go-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 覆盖
Unhappy Path 覆盖
白盒验证
不适用:本次为协议层黑盒性能/正确性验证。
回归测试
已存在并复跑 motr PR #89 的
script/14_issue_regression/issue_26845_26847_protocol_decimal.go(PR 当前 commita6a50dfcf7053027dab5e9117fde7bdd7b5cdefb)。测试证据
MOTR 连续 3 轮:该 scenario 与关联场景合计 15/15 成功、171 条断言成功;性能采样结果见原问题验证。
测试结论
测试通过。
Ariznawlll commentedon Aug 10, 2026
需要加大数据量测试,先不关闭
Ariznawlll commentedon Aug 19, 2026
Big-data 回归补充
已补充 #26845 的 big-data 回归:
script/issue_26845_prepared_decimal_null_bigdata.py。query_tests_1y,作为 main-only matrix case 执行;4.2-dev 跳过。DECIMAL(20,4)、每 7 行一个 NULL、literal/untyped NULL/typed NULL 精确计数,以及 8-client p50/QPS 对照。fix prepared DECIMAL comparison coercion (matrixorigin#26849)
fix prepared DECIMAL comparison coercion (matrixorigin#26849)
fix prepared DECIMAL comparison coercion (matrixorigin#26849)