PageHelper分页框架 自动统计总记录数性能问题优化

2022-07-30  本文已影响0人  我的小鱼干去哪儿了

1. 问题: PageHelper自动统计总记录数(即count()方法)时存在性能问题

PageHelper.startPage(request.getPage(), request.getSize(),
                      request.isReturnTotal())
                    .doSelectPageInfo(() -> getEntitiesByCondition(condition, sort));

PageHelper 目前自动count()总记录数时对于数据量大的表或者复杂SQL存在性能问题,
框架生成count SQL的方式 是 对查询SQL直接在外面包一层

 select count(0) from (select * from yousql order by xxxx)

2. 解决

可以使用自定义count SQL的方式来处理这个性能问题:

    <select id="queryData" resultMap="BaseResultMap">
        select * from table where xxxx
        <if test="sort != null">
            order by ${sort}
        </if>
    </select>

    <select id="queryData_COUNT" resultType="java.lang.Long">
        select count(0) from table where xxxx
    </select>

具体参看如下截图

wecom-temp-91147a790be857404cfb71698176a3ab.png
上一篇 下一篇

猜你喜欢

热点阅读