Files
eip4-platform/docs/sql/SYS_DOC_INDEX.sql
zhengwr 5d12613034 perf(WS-D I1): /api/file/list 去函数包列 + sys_doc 函数式部分索引(全表扫→Index Scan)
I1 性能(必修):
- 根因: GET /api/file/list 原查询 WHERE UPPER(tablename)=? AND TRIM(businesskey)=?
  AND isactive=1 —— 列被函数包裹(非 sargable),sys_doc 33.8 万行仅 PK 索引
  → 每次附件页渲染 Parallel Seq Scan 全表(amis service 容器每渲必打本端点;
  EXPLAIN cost ~18572)。
- 修(FileController.list 查询):
  · tablename 列本就全大写(DB 实测 tablename<>upper(tablename) 计数 0),app 侧已
    tableName.trim().toUpperCase() → 去 UPPER 改 tablename = ?(sargable)。
  · businesskey 不能简单 = trimmed: DB 实测 13000 行存「32 位 UUID + 4 空格 padding
    至 36」CHAR 补白遗留(业务 PK 为 character(36)),naive businesskey=trimmed 会
    漏列这 13000 行附件 → 保留 trim 语义改 rtrim(businesskey) = ?,配函数式索引使其
    sargable。全程 ? 绑定(防注入,无拼接)。
- 索引 DDL: docs/sql/SYS_DOC_INDEX.sql
    CREATE INDEX IF NOT EXISTS ix_sys_doc_biz
      ON eip3.sys_doc (tablename, rtrim(businesskey)) WHERE isactive = 1;
  已应用到 eip4dev(含 COMMENT,幂等)。客户库排切流窗口执行(与现有 docs/sql/* 同放
  同风格;P5 Flyway 待办同 SYS_PAGE_SCHEMA.sql)。
- 验证(eip4dev): EXPLAIN Seq Scan(cost 18572) → Index Scan using ix_sys_doc_biz
  (cost 8.44, exec 0.038ms); 补白键 OA_NOTICEMANAGE + 横线键 OA_NEWSMANAGE 均命中。
  8093 重启含新 jar 后 /api/file/list 两种键风格均正常返附件(含 13000 行补白键场景)。

补测试:
- FileControllerTest #5 注入安全(priority 契约): 恶意 tableName/businessKey 不抛、返空,
  ArgumentCaptor 断言走 ? 绑定(SQL 文本为静态参数化语句不含用户串,用户原串作绑定值)。
- TemplateToAmisConverterTest 新增 skip-guard: 表名 ORPHAN_*(SYS_ENTITY 解析不到)→
  enrichAttachmentFields 降级守卫生效,附件字段保留原 input-file 不包 service。
- 测试全绿: FileControllerTest 5/5, TemplateToAmisConverterTest 93/93(JDK21)。

M4: buildAttachmentField 加注释说明 service.name 与内层 input-file 同名(DOCUMENT)
  仅作布局归位 key,表单值由内层 input-file 绑定,amis 容忍(已浏览器验证)。

I2(已知系统级后续项,本次不修): /api/file/list 及 download 无行级数据权限——任意已认证
  用户可枚举任意业务记录附件,与现有 download/flow 同 posture(生产仅认证可访问)。
  TODO 注释标于 FileController.list javadoc。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 18:20:38 +08:00

35 lines
2.4 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================================================
-- 性能索引脚本:SYS_DOC 附件按业务实体检索(WS-D I1)
-- ----------------------------------------------------------------------------
-- 归属:platform-metadata(GET /api/file/list,附件查看态只读下载列表)。
-- 背景:sys_doc 33.8 万行,原仅 PK 索引(sys_doc_id)。/api/file/list 按
-- tablename + businesskey 列附件,amis service 容器每次附件页渲染都打本端点。
-- 原查询 WHERE UPPER(tablename)=? AND TRIM(businesskey)=? AND isactive=1 —— 列被
-- 函数包裹(非 sargable)→ Parallel Seq Scan 全表 33.8 万行(EXPLAIN cost ~18572)。
--
-- 修:
-- 1) tablename 列本就全大写(DB 实测:sys_doc.tablename<>upper(tablename) 计数 0),
-- app 侧已 tableName.trim().toUpperCase(),查询去 UPPER 改 tablename = ?(sargable)。
-- 2) businesskey 列 varchar(100) 存 CHAR(36) 补白遗留:DB 实测 13000 行为
-- 「32 位 UUID(去横线)+ 4 空格 padding 至 36」。不能简单 businesskey = trimmed
-- (会漏这 13000 行)→ 保留 trim 语义,查询用 rtrim(businesskey) = ?,并配
-- 函数式索引(索引 key 同为 rtrim(businesskey))使其 sargable。
-- 3) 部分索引(WHERE isactive=1)与 list 查询条件一致,索引更小更贴。
--
-- 验证(eip4dev 实测,2026-06-14):
-- EXPLAIN 走 Index Scan using ix_sys_doc_biz,cost 18572 → 8.44,
-- actual Execution Time 0.038 ms(补白键 OA_NOTICEMANAGE + 横线键 OA_NEWSMANAGE 均命中)。
--
-- 【Flyway 待办(P5)】同 SYS_PAGE_SCHEMA.sql:本工程当前未引入 Flyway,本脚本即
-- 「生产建表/建索引脚本」。客户库排切流窗口执行(CREATE INDEX 默认持表锁;若需在线
-- 无锁建索引可改 CREATE INDEX CONCURRENTLY,但不能在事务/Flyway 事务内执行)。
-- P5 引入 Flyway 时原样改名 V{n}__create_ix_sys_doc_biz.sql 纳入 db/migration 即可。
-- ============================================================================
CREATE INDEX IF NOT EXISTS ix_sys_doc_biz
ON eip3.sys_doc (tablename, rtrim(businesskey))
WHERE isactive = 1;
COMMENT ON INDEX eip3.ix_sys_doc_biz IS
'WS-D I1:/api/file/list 按 tablename + rtrim(businesskey) 检索附件的 sargable 函数式部分索引(isactive=1)';