springboot-mbatis の mysql データベースが in 条件を使用しており、その数が 1000 を超えている場合とエラー レポートの解決策
プロジェクト内のmysqlデータベースを使用し、inのクエリ条件を使用して、過剰な数を解決します。たとえば、SQL は次のとおりです。
select * from table where id in (1,2,...10000)、in の数が 1000 を超える場合、エラーが報告されます
解決策 1: 内の数値をグループ化し、複数回クエリを実行します。最初にソートすることをお勧めします。これにより、クエリ時間を短縮できます (インデックスがある場合)。
List<Long> newAllReceiveIdList = allReceiveIdList.stream().sorted().distinct().collect(Collectors.toList());
List<List<Long>> partitionReceiveIdList = Lists.partition(newAllReceiveIdList, 800);
List<ReceiveBill> receiveBillList = new ArrayList<>();
for (List<Long> singlePartitionReceiveIdList : partitionReceiveIdList) {
if(CollUtil.isNotEmpty(singlePartitionReceiveIdList)){
LambdaQueryWrapper<ReceiveBill> receiveBillLambdaQueryWrapper = new LambdaQueryWrapper<ReceiveBill>()
.in(ReceiveBill::getId, singlePartitionReceiveIdList)
.eq(ReceiveBill::getDeleted, BoolEnum.NO.getKey());
List<ReceiveBill> billList = receiveBillService.list(receiveBillLambdaQueryWrapper);
receiveBillList.addAll(billList);
}
}
解決策 2: SQL を または で区切って直接変更します。
次の SQL に変更します: select * from table where id in (1,2,...999) or (1000,1001,...1888)
プロジェクトでは mybatis フレームワークが使用されているため、各値は「,」区切り文字で区切られ、最後に余分なカンマが生じるため、null を追加する必要があります。
select * from table where id in (1,2,……998,null)または (998,1000,1001,…1888)
<if test="bo.receiveIds != null and bo.receiveIds.length != 0">
and (
f.id in
<foreach item='receiveId' index='index' collection='bo.receiveIds' open='(' separator=',' close=')'>
<if test = 'index%999== 998'>
null ) or f.id in (
</if>
#{receiveId}
</foreach>
)
</if>