批量查询 java_java批量查询

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值