for循环与mysql的分页查询
来源:互联网 发布:itools for mac录屏 编辑:程序博客网 时间:2024/06/02 12:19
这两天写了一个定时任务,关于查询表中快过期的卡,发送短信提醒,在查询中,因为生产上的数据数十万条,数据量大,如果直接一下子查出来,会对数据库造成很大的压力,用到分页查询,按照正常逻辑,查询表中总数量,然后根据当前页以及每页数量,循环对数据库进行查询;
//分页查询,每次查询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进行循环。
阅读全文
1 0
- for循环与mysql的分页查询
- MySQL与Oracle的分页查询语句
- Mysql与Oracel的分页查询
- mysql的分页查询
- mysql的分页查询
- MySql的分页查询
- oracle 与mysql分页查询
- mySQL与Oracle分页查询
- Oracle与mysql分页查询
- mysql与oracle分页查询
- HashMap<String,String>与for合作查询和for嵌套循环查询的差别和效率
- sql server,mysql与oracle常见数据库的分页查询
- 关于MySql的分页查询
- Mysql分页查询的方法
- MySQL的分页查询语句
- mysql,oracle的分页查询
- 【Servlet】利用JSTL表达式,C标签的循环与判断,对数据库查询内容进行分页
- mysql,sqlServer与oracle分页查询语句
- 怎样才能让英语课程能够在微信上预约?
- Android学习笔记三
- 解决监听label时,点击label内input执行两次的bug
- DOOM Dev:未来,异步计算将更广泛地用于idTech 6; 更多的开发者会利用它
- innodb的锁
- for循环与mysql的分页查询
- 文章标题
- wkhtmltopdf 中文参数详解
- ajax 文件上传
- Shell Programming(一)——重定向与管道
- Edittext光标移到最后
- 精彩博客
- Java 练习题
- 免费的论文查重网站