sql逻辑优化

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

java 复制代码
@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如下:

XML 复制代码
<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 查询速度较慢

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

java 复制代码
@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如下:

XML 复制代码
<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>

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

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

有好的建议,欢迎大家评论!

相关推荐
sqyno1sky28 分钟前
数据分析与科学计算
jvm·数据库·python
gjc5921 小时前
如何写好SQL:企业内训文档
数据库·sql
liulilittle1 小时前
SQLite3增删改查(C
c语言·开发语言·数据库·c++·sqlite
Astro_ChaoXu1 小时前
GAMSE使用日志与教程(高分辨率光谱数据缩减)
linux·数据库·python
新缸中之脑1 小时前
Google TurboQuant 详解
数据库·redis·缓存
yoyo_zzm2 小时前
MySQL数据库误删恢复_mysql 数据 误删
数据库·mysql·adb
F1FJJ2 小时前
Shield CLI 的 PostgreSQL 插件 v0.5.0 发布:数据库导出 + 协作增强,ER 图全新体验
网络·数据库·docker·postgresql·go
weixin199701080163 小时前
《深入浅出:图解淘宝分布式数据库TDDL(及开源替代方案)》
数据库·分布式·开源
数据库小组3 小时前
Oracle 上云 / 替代场景下,NineData 完成到 PostgreSQL 的低风险迁移
大数据·数据库·mysql·postgresql·oracle·数据一致性·数据库迁移
Ricky_Theseus3 小时前
SQL Server 2008 四种排序函数
数据库