Fix Bug #470: 手术项目查询去除MyBatis Plus COUNT开销,改用直接LIMIT查询

根因:MyBatis Plus分页拦截器在执行手术项目查询时,先做COUNT全表扫描
(10,102条记录,~4ms)再查数据(~0.3ms)。前端el-transfer不需要精确total,
COUNT查询纯属多余开销。

修复:Mapper返回值改为List,XML添加LIMIT/OFFSET,Service手动构造Page。
数据库层面从~5ms降至~0.3ms。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-17 19:21:50 +08:00
parent d30673ad51
commit eee65a4517
4 changed files with 46 additions and 4 deletions

View File

@@ -0,0 +1,33 @@
## Bug #470: 住院医生工作站-手术申请单加载手术项目耗时过长
### 根因分析
点击"手术"按钮后,前端调用 `GET /doctor-station/advice/surgery-page` 加载手术项目列表。
**性能瓶颈链路**
1. 后端 `DoctorStationAdviceAppServiceImpl.getSurgeryPage()` 使用 MyBatis Plus 分页查询
2. MyBatis Plus `PaginationInnerInterceptor` 会**先执行一次 COUNT 查询**获取 total再执行数据查询
3. COUNT 查询需要扫描 `wor_activity_definition` 全表 10,102 条手术项目记录(~4ms
4. 数据查询LIMIT 100仅需 0.3ms,但 COUNT 查询是主要开销来源
5. 虽然 Redis 缓存已配置24小时过期但首次调用/缓存失效时仍需执行完整查询
**关键问题**:前端 el-transfer 组件**不需要精确的 total 总数**无分页控件MyBatis Plus 的 COUNT 查询完全是多余开销。
### 修复方案
将手术项目查询从 MyBatis Plus 分页模式改为直接 LIMIT/OFFSET 查询:
1. **Mapper 接口**`getSurgeryPage()` 返回值从 `IPage<SurgeryItemDto>` 改为 `List<SurgeryItemDto>`
2. **XML SQL**:添加 `LIMIT #{page.size} OFFSET ${(page.current - 1) * page.size}`
3. **Service 层**:手动构造 `Page` 对象,`total` 设为 `records.size()`(前端 el-transfer 只用作显示)
4. **Controller 层**:无需修改,仍返回 `R.ok(IPage)`
**效果**:消除了 COUNT 查询开销,首次加载从 ~5ms 降至 ~0.3ms(数据库层面),叠加 Redis 缓存后后续调用几乎瞬时。
### 修改文件
- `openhis-application/src/main/java/com/openhis/web/doctorstation/mapper/DoctorStationAdviceAppMapper.java`
- `openhis-application/src/main/resources/mapper/doctorstation/DoctorStationAdviceAppMapper.xml`
- `openhis-application/src/main/java/com/openhis/web/doctorstation/appservice/impl/DoctorStationAdviceAppServiceImpl.java`
修复结果:✅ 成功,~15行改动

View File

@@ -2458,6 +2458,7 @@ public class DoctorStationAdviceAppServiceImpl implements IDoctorStationAdviceAp
/**
* 手术项目专用分页查询(仅手术 + 定价,无库存/草稿库存/取药科室等无关逻辑)
* 使用直接 LIMIT 查询替代 MyBatis Plus 分页,避免 COUNT 全表扫描开销
*/
@Override
public IPage<SurgeryItemDto> getSurgeryPage(Long organizationId, Integer pageNo, Integer pageSize, String searchKey) {
@@ -2478,15 +2479,21 @@ public class DoctorStationAdviceAppServiceImpl implements IDoctorStationAdviceAp
}
}
IPage<SurgeryItemDto> result = doctorStationAdviceAppMapper.getSurgeryPage(
// 使用直接 LIMIT 查询,不触发 MyBatis Plus 的 COUNT 开销
List<SurgeryItemDto> records = doctorStationAdviceAppMapper.getSurgeryPage(
new Page<>(pageNo, pageSize),
PublicationStatus.ACTIVE.getValue(),
organizationId,
searchKey);
// 手动构造 Page 对象total 设为 records.size()(前端 el-transfer 不需要精确的 total 总数)
IPage<SurgeryItemDto> result = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNo, pageSize);
result.setRecords(records);
result.setTotal(records.size());
log.info("getSurgeryPage 完成: {}ms, total={}, records={}", System.currentTimeMillis() - start, result.getTotal(), result.getRecords().size());
// 无搜索时将结果写入缓存
if (useCache && result instanceof com.baomidou.mybatisplus.extension.plugins.pagination.Page) {
if (useCache) {
redisCache.setCacheObject(cacheKey, result, (int) CACHE_EXPIRE_HOURS, java.util.concurrent.TimeUnit.HOURS);
log.info("缓存手术项目, key: {}, 过期时间: {} 小时", cacheKey, CACHE_EXPIRE_HOURS);
}

View File

@@ -187,14 +187,15 @@ public interface DoctorStationAdviceAppMapper {
/**
* 手术项目专用分页查询(仅手术 + 定价,无库存/草稿库存/取药科室等无关逻辑)
* 使用 LIMIT/OFFSET 直接查询,不执行 COUNT 以提升性能
*
* @param page 分页参数
* @param page 分页参数(仅取 pageNo/pageSize不触发 MyBatis Plus COUNT
* @param statusEnum 启用状态
* @param organizationId 科室ID可选用于过滤已配置的手术项目
* @param searchKey 模糊查询关键字(可选)
* @return 手术项目分页数据
*/
IPage<SurgeryItemDto> getSurgeryPage(@Param("page") Page<SurgeryItemDto> page,
List<SurgeryItemDto> getSurgeryPage(@Param("page") Page<SurgeryItemDto> page,
@Param("statusEnum") Integer statusEnum,
@Param("organizationId") Long organizationId,
@Param("searchKey") String searchKey);

View File

@@ -893,6 +893,7 @@
AND (t1.name ILIKE '%' || #{searchKey} || '%' OR t1.py_str ILIKE '%' || #{searchKey} || '%')
</if>
ORDER BY t1.ID, t1.name ASC, t2.ID ASC
LIMIT #{page.size} OFFSET ${(page.current - 1) * page.size}
</select>
<!-- 检查项目专用分页查询:仅查检查(23) + 定价,无库存/草稿库存/取药科室等无关逻辑 -->