mysql优化---订单查询优化:异步分页处理
Posted 聚变归来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql优化---订单查询优化:异步分页处理相关的知识,希望对你有一定的参考价值。
订单分页查询:
老的代码是顺序执行查询数据和计算总记录数,但是如果条件复杂的话(比如关联子表)查询的时间要超过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 - 1) * 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
以上是关于mysql优化---订单查询优化:异步分页处理的主要内容,如果未能解决你的问题,请参考以下文章