sql逻辑优化-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1.分页 通常使用每页条数及第一页作为参数 开发接口

@GetMapping("/querySystemList")
public List<SystemAduit> querySystemList(@RequestParam("keyword") String keyword,
                                         @RequestParam(name = "offset", defaultValue = "0") int offset,
                                         @RequestParam(name = "limit", defaultValue = "20") int limit){
     HashMap<String,Object> params=new HashMap<>();
     if(StringUtils.isNotEmpty(keyword)){
         params.put("keyword",keyword);
     }
     params.put("limit",limit);
     params.put("offset",offset);
     List<SystemAduit> systemAduits = systemAduitMapper.querySystemList(params);
     return systemAduits;
}

SQL如下

<select id="querySystemList" resultMap="systemAduitMap" parameterType="java.util.HashMap">
	select * from system_aduit
	where 1=1
	<if test="keyword!=null and keyword!=''">			
        and opertion  like concat('%',#{keyword},'%')
	</if>
	limit #{offset},#{limit}
</select>

但是当数据量特别大的时候查询速度会减慢很多limit 10000,10  查询速度较慢

于是在查询下一页数据时 将上一页的最大值当成参数作为查询条件进行查询

@GetMapping("/queryListByParams")
public List<SystemAduit> queryListByParams(@RequestParam("keyword") String keyword,
                                           @RequestParam("maxTime") String maxTime,
                                           @RequestParam("limit") int limit){
     HashMap<String,Object> params=new HashMap<>();
     if(StringUtils.isNotEmpty(keyword)){
          params.put("keyword",keyword);
     }
     if(StringUtils.isNotEmpty(maxTime)){
          params.put("maxTime",maxTime);
     }
     params.put("limit",limit);
     List<SystemAduit> systemAduits = systemAduitMapper.queryListByParams(params);
     return systemAduits;
}

SQL如下

<select id="queryListByParams" resultMap="systemAduitMap" parameterType="java.util.HashMap">
	select * from system_aduit
	where 1=1
	<if test="keyword!=null and keyword!=''">
		and opertion  like concat('%',#{keyword},'%')
	</if>
	<if test="maxTime!=null and maxTime!=''">
		and create_time  &gt; #{maxTime}
	</if>
	limit #{limit}
</select>

当数据量特别大的话查询的速度 还是比较稳定的。

最近接触的项目若是数据量很大时则限制查询日期为一个月或者定期将数据进行备份到另一个数据库后查询该库

有好的建议欢迎大家评论

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6