pagehelper 联表 分页查询性能优化 -- (sql允许先分页,后联表)

本文介绍了如何使用 PageHelper 进行联表查询的性能优化,提出了改造 PageHelper 的思路,包括利用5.0.0以上版本的手动COUNT查询,自定义拦截器对SQL进行改造,重新定位分页参数。改造后,可以在不影响结果集的情况下,先进行分页再联表补充字段。详细步骤和代码示例可供参考。
摘要由CSDN通过智能技术生成

优化的前提条件,分页的sql允许先分页,联表并不影响最后的结果集,只是补齐所需字段信息.

一,pagehelper改造思路
     1,5.0.0以上版本支持手动count查询语句.详见 https://github.com/pagehelper/Mybatis-PageHelper/
     2,自定义拦截器,对有别名为"AS limitable" 的子查询sql进行sql拼接,将 本应拼接到末尾的limit ?,? 拼接到子查询sql末尾.
     3,重新定位入参的位置.将limit的参数从末尾调整到limit子查询语句的地方.
     4,用法,同pagehelper正常用法(版本要求5.0.4以上), 将pagehelper拦截器配置为自定义拦截器.

二,代码部分
    1,mapper

<!-- 分页条件查询  优化 -->
	<select id="findUserCount" resultMap="BaseResultMap">
		select limitable.openId as openId,
		insert(limitable.mobile,4,4,'****')          AS mobile,
		limitable.subscribeTime as subscribeTime,
		limitable.unSubscribeTime as unSubscribeTime,
		c.chanelName as chanelName
		from
		(select countBind.bindChannelId,countBind.openId,
		countBind.mobile,countBind.subscribeTime,
		countBind.unSubscribeTime,countBind.userTag
		FROM countBind  
		WHERE
		countBind.countId = #{countId,jdbcType=INTEGER}
		<if
			test="userCountReq.unSubendTime != null and userCountReq.unSubendTime !=''">
			and countBind.unSubscribeTime &lt;= #{userCountReq.unSubendTime}
		</if>
		<if
			test="userCountReq.bindChannelIds != null and userCountReq.bindChannelIds.size()>0">
			and countBind.bindChannelId in
			<foreach collection="userCountReq.bindChannelIds" index="index"
				item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test="userCountReq.tags != null and userCountReq.tags.size()>0">
			and countBind.userTag in
			<foreach collection="userCountReq.tags" index="index" item="tagItem"
				open="(" separator="," close=")">
				#{tagItem}
			</foreach>
		</if>
		) AS limitable
		left join
		channel c
		on
		limitable.bindChannelId=c.channelId
		where
		c.countId = #{countId,jdbcType=INTEGER} 
	</select>
	
	<!-- 分页条件统计Count  优化-->
	<select id="findUserCount_COUNT" resultType="java.lang.Integer">
		select count(*)
		from
		countBind b		
		where
		b.countId = #{countId,jdbcType=INTEGER}
		<if
			test="userCountReq.unSubendTime != null and userCountReq.unSubendTime !=''">
			and b.unSubscribeTime &lt;= #{userCountReq.unSubendTime}
		</if>
		<if
			test="userCountReq.bindChannelIds != null and userCountReq.bindChannelIds.size()>0">
			and b.bindChannelId in
			<foreach collection="userCountReq.bindChannelIds" index="index"
				item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test="userCountReq.tags != null and userCountReq.tags.size()>0">
			and b.userTag in
			<foreach collection="userCountReq.tags" index="index" item="tagItem"
				open="(" separator="," close=")">
				#{tagItem}
			</foreach>
		</if>
	</select>

     2 pagehelper拦截器
 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值