订单分页查询:

老的代码是顺序执行查询数据和计算总记录数,但是如果条件复杂的话(比如关联子表)查询的时间要超过20s种

    public static PagedList<Map<String, Object>> query(ITemplateService service, Identity tenantId, Identity userId, String entityName,
Map<String, Object> params, String columns, TCondition cond) {
int page = WebHelper.getPageNo(params);
int pageSize = WebHelper.getPageSize(params);
String orderColumn = (String) params.get(JqgridConstant.ORDER_COLUMN); // 排序字段
String orderSord = (String) params.get(JqgridConstant.ORDER_METHOD); // 排序方式,desc or asc handleOrderByColumn(cond, orderColumn, orderSord);
int totalCount = service.getBaseService().query4Count(tenantId, entityName, cond);
List<Map<String, Object>> list = service.query(columns, entityName, cond, tenantId, userId, pageSize, page);
Translator.prepare(list, tenantId, service); // TODO
return new PagedList<>(page, pageSize, totalCount, list);
}

优化方法:

1.通过新启动一个线程来同时做之前需要顺序执行的两个Sql查询,最后等待全部计算完成,统一进行返回

2.对于一些特别复杂的条件的查询,如果内容的条数少于PageSize,那么计算总条数的sql就是不需要执行,可以用返回的list的szie当做总记录数

public static PagedList<Map<String, Object>> queryAsyn(ITemplateService service, Identity tenantId, Identity userId, String entityName,
Map<String, Object> params, String columns, TCondition cond) {
int page = WebHelper.getPageNo(params);
int pageSize = WebHelper.getPageSize(params);
String orderColumn = (String) params.get(JqgridConstant.ORDER_COLUMN); // 排序字段
String orderSord = (String) params.get(JqgridConstant.ORDER_METHOD); // 排序方式,desc or asc
ExecutorService slaver = Executors.newSingleThreadExecutor();
FutureTask<Integer> totalCountFuture = new FutureTask<>(new TotalCountJob(service, tenantId, entityName, cond));
slaver.execute(totalCountFuture);
handleOrderByColumn(cond, orderColumn, orderSord);
slaver.shutdown();
//主线程来取数据
long time1 = System.nanoTime();
List<Map<String, Object>> list = service.query(columns, entityName, cond, tenantId, userId, pageSize, page);
long time2 = System.nanoTime();
long diff = time2 - time1;
logger.debug("查询方案统计-----查询分页list部分,用时:{}s,条件:{}", translateToSecond(diff), cond);
Integer totalCount = null;
int listSize = list.size();
if (listSize < pageSize) {
logger.info("本次查询不需要sql进行count操作");
totalCount = listSize + (page - ) * pageSize;
slaver.shutdownNow();
} else {
try {
//没做完就等着
totalCount = totalCountFuture.get();
} catch (Exception e) {
totalCountFuture.cancel(true);
logger.error("totalCount发生异常", e);
}
} Translator.prepare(list, tenantId, service);
return new PagedList<>(page, pageSize, totalCount, list);
} private static double translateToSecond(long diff) {
return diff * 0.000000001;
} static class TotalCountJob implements Callable<Integer> { private String tableName;
private TCondition condition;
private Identity tenantId;
private ITemplateService service; public TotalCountJob(ITemplateService service, Identity tenantId, String tableName, TCondition condition) {
this.service = service;
this.tableName = tableName;
this.condition = condition;
this.tenantId = tenantId;
} @Override
public Integer call() throws Exception {
long time1 = System.nanoTime();
Integer totalCount = service.getBaseService().query4Count(tenantId, tableName, condition);
long time2 = System.nanoTime();
long diff = time2 - time1;
logger.debug("查询方案统计-----查询分页count部分,用时:{}s,条件:{}", translateToSecond(diff), condition);
return totalCount;
}
}

这是第一次优化的文章,欢迎访问:

http://www.cnblogs.com/victor2302/p/6073821.html

  

最新文章

  1. Beyond Compare 2
  2. sql 通过游标 拆分xml结构
  3. android中页面的返回刷新
  4. RACSignal的Subscription深入
  5. Zepto 使用中的一些注意点(转)
  6. 《java入门第一季》之java语法部分小案例
  7. 微信小程序之canvas 文字断行和省略号显示
  8. linux btrfs文件系统管理与应用
  9. java提高(6)---Serializable
  10. 快看Sample代码,速学Swift语言(3)-运算符
  11. 201621123002《JAVA程序设计》第五周学习总结
  12. Git 常用命令和统计代码量
  13. 领扣-120 三角形最小路径和 Triangle MD
  14. SpringBoot配置属性之NOSQL
  15. 解决微服务网关Ocelot使用AddStoreOcelotConfigurationInConsul后请求404问题
  16. CentOS7.4 关闭firewall防火墙,改用iptables
  17. 【uoj#282】长度测量鸡 结论题
  18. mac如何运行vue项目
  19. Linux-&gt;ZooKeeper集群搭建
  20. PAT A1060 (Advanced Level) Practice

热门文章

  1. nio原理和示例代码
  2. linux修改时间显示格式
  3. android_layout_relativelayout(二)
  4. 【小家Spring】老项目迁移问题:@ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?
  5. Gin 框架 - 使用 logrus 进行日志记录
  6. wpf怎么绑定多个值,多个控件
  7. Spring Cloud Alibaba | Sentinel: 服务限流高级篇
  8. hdu6406 Taotao Picks Apples(线段树)
  9. 三、SQL server 2008数据库的备份与还原
  10. Linux mysql开启远程访问