for循环与mysql的分页查询

在处理大数据量时,一次性查询所有数据会增加数据库压力。通过分页查询,配合for循环实现数据分批加载。当每批查询结果不足设定值时,使用break跳出循环,有效避免无效查询,简化逻辑并提高性能。同时介绍了Java中的break和continue语句在循环控制中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天写了一个定时任务,关于查询表中快过期的卡,发送短信提醒,在查询中,因为生产上的数据数十万条,数据量大,如果直接一下子查出来,会对数据库造成很大的压力,用到分页查询,按照正常逻辑,查询表中总数量,然后根据当前页以及每页数量,循环对数据库进行查询;

		//分页查询,每次查询1000条数据
		int pageSize = 1000;//每页数量
		int page = 1;//定义总页数
		int offset;//当前页起始数
		int rowCount;//当前页结束数
		List<GiftCard> list = null;
		List<Integer> userIdList = new ArrayList<Integer>();
		//得到总页数
		if(giftCardCount > 0){			
			if(giftCardCount%pageSize==0){
				page=(int) (giftCardCount/pageSize);
			}else{
				page=(int) (giftCardCount/pageSize+1);
			}
		}
		//查询出还有三天且绑定了用户的优选卡信息
		for (int i = 1; i < page + 1; i++) {			
			if(page == 1){//即将过期账号查询只有一页时
				offset = 0;
				rowCount = (int) giftCardCount;
				list = giftcardDAO.getGiftCardThreeDaysDue(rowCount,offset,currentTime,fourDaysTime);
				if(null != list && list.size() > 0) {
					for (GiftCard giftCard : list) {
						Integer userId = giftCard.getUserId();
						userIdList.add(userId);
					}			
				}
			}else{//即将过期优选卡查询多页时分页处理
				if(i == 1){					
					offset = 0;
				}else{
					offset = (i-1)*pageSize;					
				}
				if(page == i){					
					//rowCount = (int) giftCardCount;
					rowCount = (int) giftCardCount - (i-1) * pageSize;
				}else {
					//rowCount = i*pageSize -1;
					rowCount = pageSize;
				}
				list = giftcardDAO.getGiftCardThreeDaysDue(rowCount,offset,currentTime,fourDaysTime);
				if(null != list && list.size() > 0) {
					for (GiftCard giftCard : list) {
						Integer userId = giftCard.getUserId();
						userIdList.add(userId);
					}			
				}
			}
		}

上面的代码,是否能进一步优化呢?

如果采用for循环的break跳出会不会逻辑更加简单些?

每一次循环查出1000条,当某一次查出的数据的数量不够1000条时,表示再往下循环已然没有数据,这个时候跳出,循环结束,即将过期卡的信息也被完全查出,相比上面的业务逻辑,简单许多,在生产商可以节省不少时间,修改代码如下:

		//分页查询,每次查询1000条数据
		int pageSize = 1000;//每页数量		
		int offset;//当前页起始数	
		List<GiftCard> list = null;
		List<Integer> userIdList = new ArrayList<Integer>();
		
		for(int i = 0;i>=0;i++){
			offset = i*pageSize;
			list = giftcardDAO.getGiftCardThreeDaysDue(pageSize,offset,currentTime,fourDaysTime);
			if(null != list && list.size() > 0) {
				for (GiftCard giftCard : list) {
					Integer userId = giftCard.getUserId();
					userIdList.add(userId);
				}			
			}
			if(null != list && list.size() > 0 && list.size() < pageSize){
				break;
			}
		}
这里利用了for循环的break跳出,优化代码简化逻辑,而且减少了对数据库的访问次数。

补充:这里也顺便介绍下java跳出for循环的break和continue语句;

“break”语句用来结束循环,即不再执行后边的所有循环。

示例:计算1+2+3+4......+100的结果。

public class example1{
    public static void main(String[] args){
        int result=0;
        for(int i=1;i<=100;i++){
            if(i>50) break;
            result+=i;
        }
        System.out.println(result);
    }
}
输出结果:
1275

分析:程序只计算了1+2+3+4+......+50的结果,后边的循环全部没有执行,即当i=51的时候,循环就结束了。

另外,“break”语句可以与switch开关语句一起使用, 当break用于开关语句switch中时,可使程序跳出switch而执行switch以后的语句;如果没有break语句,则会从满足条件的地方(即与switch(表达式)括号中表达式匹配的case)开始执行,直到switch结构结束。

“continue”语句用来结束当前循环,并进入下一次循环,即仅仅这一次循环结束了,不是所有循环结束了,后边的循环依旧进行。

示例:计算1+2+3+4......+100的结果。

public class example1{
    public static void main(String[] args){
        int result=0;
        for(int i=1;i<=100;i++){
            if(i>50&&i<=60) continue;
            result+=i;
        }
        System.out.println(result);
    }
}
输出结果:
4495

分析:程序计算了1+2+3+......+48+49+50+61+62+63+......+100的结果,仅仅没有对i=51,52......60进行循环。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值