candy 发表于 2018-10-22 07:45:43

由一次long SQL调优引发de思考

public void execute() {  
      //获取线程数,默认30
  
      int threadNum = 30;
  
      String strThreadNum = LionConfigUtils.getProperty("ts-monitor-job.dailyJob.accountBalanceDailyCheckerThreadNum",
  
                "");
  
      if (isNumeric(strThreadNum)) {
  
            threadNum = Integer.parseInt(strThreadNum);
  
      }
  
      monitorLogger.info(String.format("BATCH_SIZE:%s, Thread number:%s", BATCH_SIZE, threadNum));
  

  
      ExecutorService service = Executors.newFixedThreadPool(threadNum);
  
      //所有账号总数
  
      int accountCount = accountDao.findAllMerchantAccountCount();
  
      int latchCount = accountCount % BATCH_SIZE == 0 ? accountCount / BATCH_SIZE : (accountCount / BATCH_SIZE + 1);
  
      CountDownLatch latch = new CountDownLatch(latchCount);
  
      Date bizDate = DateUtils.addDate(DateUtils.removeTime(new Date()), -1);
  
      Date lastBizDate = DateUtils.addDate(bizDate, -1);
  
      //起始偏移
  
      int offset = 0;
  
      //上一页中最大的ID
  
      int lastBiggestId = 0;
  
      List accountDataList = null;
  
      while (offset < accountCount) {
  
            accountDataList = accountDao.findAllMerchantAccount(offset, BATCH_SIZE, lastBiggestId);
  
            if(accountDataList == null){
  
                break;
  
            }
  
            //取出当前批次中最大的id供下次取数据使用,SQL分页查询优化
  
            lastBiggestId = accountDataList.get(accountDataList.size() - 1).getId();
  
            //偏移后移,取下一页
  
            offset += BATCH_SIZE;
  
            service.submit(new DoAccountMonitorThread(accountDataList, bizDate, lastBizDate,monitorAccountBalanceDao, accountEntryDao, latch));
  
      }
  
      try {
  
            latch.await();
  
      } catch (InterruptedException e) {
  
            monitorLogger.error("CountDownLatch.await() error:" + e);
  
      }
  
    }


页: [1]
查看完整版本: 由一次long SQL调优引发de思考