fix:出入库记录查询速度优化

This commit is contained in:
zhaoyf
2026-11-27 14:05:59 +08:00
parent eb6d871fe0
commit d6d98da8be
3 changed files with 67 additions and 14 deletions

View File

@@ -20,4 +20,11 @@ import java.util.Map;
public interface StIvtStructivtflowMapper extends BaseMapper<StIvtStructivtflow> {
List<Map> getPageQuery(@Param("query") StructIvtFlowQuery query, PageQuery pageQuery);
/**
* 按查询条件统计总数。
* 只扫主表 st_ivt_structivtflow,关联表条件用 EXISTS 表达,
* 不做多表 join 后再 count,避免流水表大数据量下统计过慢。
*/
long countPageQuery(@Param("query") StructIvtFlowQuery query);
}

View File

@@ -13,13 +13,39 @@
left join st_ivt_structattr struct on struct.struct_code = struct_flow.struct_code
left join md_me_materialbase material on struct_flow.material_id = material.material_code
left join md_pb_measureunit unit on struct_flow.unit_id = unit.measure_unit_id
<include refid="queryWhere"/>
ORDER BY struct_flow.id DESC
</select>
<!--
总数单独统计:不走分页插件的 count。
只 COUNT 主表,关联表条件用 EXISTS 表达,统计时不需要 join 后再 count,
在几百万行的流水表上比分页插件的 count 快很多。
条件必须与 queryWhere 保持同源,故两个语句共用该片段。
-->
<select id="countPageQuery" resultType="long">
SELECT COUNT(1)
FROM st_ivt_structivtflow struct_flow
<include refid="queryWhere"/>
</select>
<!-- 列表与总数共用的查询条件,注意别名:struct_flow 为主表,子查询内使用独立别名 -->
<sql id="queryWhere">
<where>
<if test="query.search != null and query.search != ''">
and (struct.struct_code LIKE '%${query.search}%'
or struct.struct_name LIKE '%${query.search}%')
and exists (
select 1 from st_ivt_structattr sa
where sa.struct_code = struct_flow.struct_code
and (sa.struct_code like concat('%', #{query.search}, '%')
or sa.struct_name like concat('%', #{query.search}, '%'))
)
</if>
<if test="query.material_code != null and query.material_code != ''">
and material.material_code LIKE '%${query.material_code}%'
and exists (
select 1 from md_me_materialbase mm
where mm.material_code = struct_flow.material_id
and mm.material_code like concat('%', #{query.material_code}, '%')
)
</if>
<if test="query.sect_code != null and query.sect_code != ''">
and struct_flow.sect_code = #{query.sect_code}
@@ -28,17 +54,17 @@
and struct_flow.stor_code = #{query.stor_code}
</if>
<if test="query.vehicle_code != null and query.vehicle_code != ''">
and struct_flow.vehicle_code LIKE '%${query.vehicle_code}%'
and struct_flow.vehicle_code like concat('%', #{query.vehicle_code}, '%')
</if>
<if test="query.pcsn != null and query.pcsn != ''">
and struct_flow.pcsn = '${query.pcsn}'
and struct_flow.pcsn = #{query.pcsn}
</if>
<if test="query.start_time != null and query.start_time != ''">
and struct_flow.update_time >= #{query.start_time}
and struct_flow.update_time &gt;= #{query.start_time}
</if>
<if test="query.end_time != null and query.end_time != ''">
and #{query.end_time} >= struct_flow.update_time
and struct_flow.update_time &lt;= #{query.end_time}
</if>
</where>
</select>
</sql>
</mapper>

View File

@@ -1,8 +1,8 @@
package org.nl.wms.warehouse_manage.record.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.nl.common.domain.query.PageQuery;
import org.nl.wms.warehouse_manage.record.service.IStIvtStructivtflowService;
@@ -11,6 +11,7 @@ import org.nl.wms.warehouse_manage.record.service.dao.mapper.StIvtStructivtflowM
import org.nl.wms.warehouse_manage.record.service.dto.StructIvtFlowQuery;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -25,14 +26,33 @@ import java.util.Map;
@Service
public class StIvtStructivtflowServiceImpl extends ServiceImpl<StIvtStructivtflowMapper, StIvtStructivtflow> implements IStIvtStructivtflowService {
/** 默认分页大小,与 PageQuery 的默认值保持一致 */
private static final int DEFAULT_PAGE_SIZE = 100;
@Override
public Object pageQuery(StructIvtFlowQuery query, PageQuery pageQuery) {
com.github.pagehelper.Page<Object> page = PageHelper.startPage(pageQuery.getPage() + 1, pageQuery.getSize());
page.setOrderBy("id DESC");
List<Map> mst_detail = this.baseMapper.getPageQuery(query, pageQuery);
int pageNum = ObjectUtil.defaultIfNull(pageQuery.getPage(), 0) + 1;
int pageSize = ObjectUtil.defaultIfNull(pageQuery.getSize(), DEFAULT_PAGE_SIZE);
if (pageNum < 1) {
pageNum = 1;
}
if (pageSize < 1) {
pageSize = DEFAULT_PAGE_SIZE;
}
// 总数单独统计:只 COUNT 主表,不让分页插件把多表 left join 的结果集包成子查询再 count
long total = this.baseMapper.countPageQuery(query);
List<Map> records = Collections.emptyList();
if (total > 0) {
// 总数已单独查过,这里关闭分页插件的 count,只保留 LIMIT 拼接
PageHelper.startPage(pageNum, pageSize, false);
records = this.baseMapper.getPageQuery(query, pageQuery);
}
com.baomidou.mybatisplus.extension.plugins.pagination.Page<Map> result =
new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageQuery.getPage() + 1, pageQuery.getSize(), page.getTotal());
result.setRecords(mst_detail);
new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize, total);
result.setRecords(records);
return result;
}
}