我有一个包含70个元素的列表.
例如:
List<Long> dbList = new ArrayList<Long>();
dbList有70条记录.如果我将所有记录发送到MySql中的查询,则需要很长时间.所以现在我想每次向数据库查询发送10个元素.所以我需要以10为间隔迭代列表.我该怎么做?这是避免长时间使用的好方法IN.
SQL查询
select model.boothId, model.panchayat.panchayatId
from Table1 model
where model.panchayat.panchayatId in(:locationValues)
and model.publicationDate.publicationDateId in (:publicationDateIdsList)
and model.constituency.id = :id group by model.panchayat.panchayatId
Run Code Online (Sandbox Code Playgroud)
提前致谢...
Mar*_*nik 13
ArrayList#subList是一个非常有效的操作.您可以遍历大小为10的范围:
for (int i = 0; i < dbList.size(); i += 10) {
List<Long> sub = dbList.subList(i, Math.min(dbList.size(),i+10)));
... query ...
}
Run Code Online (Sandbox Code Playgroud)