在SQL Server或者MySQL中,当你在IN子句中需要处理的结果集可能超过2100个元素时,由于SQL Server对IN子句中的参数数量存在大约2100个左右的限制,直接使用IN会导致错误。为了解决这个问题,可以采取以下策略:
1、将参数拆分,分批次查询出结果然后合并
2、建立临时表批量插入IN参数 join 或者 exists 查询后删除掉
我一般都是将参数表一直留着,插入时候带入时间戳参数,查询时候按时间戳查,
后面跑任务定时批量清除
参考代码
QueryWrapper<EVVipset> wrapper = new QueryWrapper();
List<String> includeList=term.getIncludeList();
if(!isListNotEmpty(includeList)) return new ArrayList<>();
int includeSize=includeList.size();
if(includeSize>2000){
String guid= UUID.randomUUID().toString();
List<TempForQuery> forQueryList=new ArrayList<>();
for(String dm:includeList){
forQueryList.add(new TempForQuery(guid,"vipset",dm));
}
tempForQueryMapper.insertBatch(forQueryList);
wrapper.exists("select 1 from TempForQuery WITH(NOLOCK) where guid='"+guid+
"' and type='vipset' and code=V_VIPSET.dm");
}else{
wrapper.in("dm",includeList);
}
List<EVVipset> list = vipsetMapper.getCustList(wrapper);
return list;