Commit 05477cfd by 张富康

fix(rpc): 修正BaseGoodsMapper.xml中thirdParty字段查询错误

- 将third_party_name字段查询条件中的表别名由b改为c
- 将third_party_code字段查询条件中的表别名由b改为c
- 修复了因表别名错误导致的查询异常问题
1 parent c8e231c3
...@@ -830,11 +830,11 @@ ...@@ -830,11 +830,11 @@
</if> </if>
<if test="thirdPartyName != null and thirdPartyName != ''"> <if test="thirdPartyName != null and thirdPartyName != ''">
and b.third_party_name like concat('%',#{thirdPartyName},'%') and c.third_party_name like concat('%',#{thirdPartyName},'%')
</if> </if>
<if test="thirdPartyCode != null and thirdPartyCode != ''"> <if test="thirdPartyCode != null and thirdPartyCode != ''">
and b.third_party_code like concat('%',#{thirdPartyCode},'%') and c.third_party_code like concat('%',#{thirdPartyCode},'%')
</if> </if>
group by a.id group by a.id
......
  • @wangxw1
    commented
    Master

    你好。我是负责本次代码审查的资深软件开发工程师。本次审查针对你提交的 BaseGoodsMapper.xml 修改内容,重点关注功能正确性、性能风险及工程最佳实践。

    以下是针对本次代码变更的审查意见:

    一、关键问题与优化建议(Top 3)

    1. 性能风险:模糊查询导致全表扫描

      • 问题描述:代码中使用了 like concat('%', #{thirdPartyName}, '%')。在 SQL 中,以通配符 % 开头的模糊查询会导致索引失效。
      • 影响分析:随着 goods 表或关联表数据量的增长,该查询将触发全表扫描(Full Table Scan),显著增加数据库 I/O 压力和响应延迟,可能导致线上接口超时。
      • 优化建议:
        • 如果业务允许,建议改为后置匹配:like concat(#{thirdPartyName}, '%')。
        • 若必须全模糊搜索,且数据量较大(如超过 100 万行),建议引入搜索引擎(如 Elasticsearch)或使用数据库的空间索引/全文索引。
    2. 可维护性:使用无意义的表别名

      • 问题描述:SQL 中使用了 a, b, c 等单字母别名。本次修复正是为了纠正别名引用错误(将 b 改为 c)。
      • 影响分析:单字母别名缺乏语义化,在多表关联(Join)较多的复杂查询中,极易发生误写(如本次修复的问题)且难以被 Code Review 发现。长期维护成本高。
      • 优化建议:使用具有业务含义的缩写作为别名。例如:goods (g), category (cat), third_party (tp)。 xml <!-- 优化示例 --> <if test="thirdPartyName != null and thirdPartyName != ''"> and tp.third_party_name like concat('%',#{thirdPartyName},'%') </if>
    3. 健壮性风险:GROUP BY 字段不完整隐患

      • 问题描述:代码末尾使用了 group by a.id。
      • 影响分析:如果在 SELECT 列表中包含了非聚合字段,且这些字段不属于 a.id(主键)的函数依赖列,在 MySQL 开启 ONLY_FULL_GROUP_BY 模式时会抛出异常。即使未开启,获取到的非聚合列数据也具有不确定性。
      • 优化建议:核查 SELECT 投影列。确保所有非聚合查询列都包含在 GROUP BY 子句中,或者使用 ANY_VALUE() 函数(MySQL 5.7+)明确取值意图。

    二、评分明细

    评分维度 分数 理由
    功能实现的正确性与健壮性 36/40 修复了明显的别名引用错误,逻辑转为正确。但 group by 的用法在严谨性上存在隐患。
    安全性与潜在风险 30/30 使用了 #{} 参数绑定及 concat 函数,有效防止了 SQL 注入风险。
    最佳实践与可维护性 14/20 提交修复及时,但表别名命名不符合清晰易读的工程实践,导致了本次 Bug 的产生。
    性能与资源利用 3/5 模糊查询的通配符位置存在严重的性能隐患,大数据量下表现不佳。
    提交信息质量 5/5 Commit 信息规范,清晰描述了修改内容、原因及作者,符合最佳实践。

    总分:88分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!