1、limit方法
int count = customerRepository.getCustomersCount(userId);//总数量
int pageSize = 1000;//每次查询的数量
int pageNums = count / pageSize + 1;//查询次数
int page = 0;
ExecutorService threadPool = Executors.newFixedThreadPool(9);
long start = System.nanoTime();
//查找店铺所有会员
while (page < pageNums){
List customerIds = customerRepository.getCustomerIdsByPageNum(userId, page, pageSize);
//select customer_id from customer limit pageNum * pageSize , pageSize
for (Long customerID:customerIds) {
threadPool.execute(
() -> customerSwitchListenner.handleCustomer(userId, customerID,unitPrice)
);
}
page++;
}
threadPool.shutdown();
但是这种方法再数据量大的时候,limit性能会比较低
2、根据id查
long startID = 0;
while(true){
//select * from user where id >= startID limit 1000
List list = userReposity.getUser(startID);
if(list.size()==0){
break;
}
for(User user : list){
startID = user.getID()+1;
}
}
转载至链接:https://my.oschina.net/u/3670641/blog/1933161