데이터베이스/SQL
[오라클] ORA-01795 IN 절에 사용가능한 값의 개수 제한
Reference M1
2023. 9. 19. 11:38
List로 주어진 여러 개의 조건 값을 IN 형태로 조건을 경우가 생긴다. 일반적인 상황이라면은 문제가 없지만 1000건 이상으로 IN절을 구성할 때는 ORA-01795 오류를 확인할 수 있다.
ORA-01795: 목록에 지정 가능한 식의 최대 수는 1000입니다 (maximum number of expressions in a list is 1000)
<select id="selectItems">
SELECT *
FROM TBL
WHERE CDN IN
<foreach collection="items" item="item" open="(" close=")" separator=", ">
#{item.val}
</foreach>
</select>
해결 방법
1. OR 연산자 1000건씩 분리
<select id="selectItems">
SELECT *
FROM TBL
WHERE
<foreach collection="partitionedItems" item="partition" separator="OR">
CDN IN
<foreach collection="partition" item="item" open="(" close=")" separator=", ">
#{item.val}
</foreach>
</foreach>
</select>
List<List<T>> partitionedItems = new ArrayList<>(
items.stream()
.collect(Collectors.groupingBy(item -> item / 1000))
.values()
);
2. IN절 내부 SubQuery
텍스트 파라미터는 1000개 제한이 있지만 SubQeury는 제한이 없다.
<select id="selectItems">
SELECT *
FROM TBL
WHERE CDN IN
<foreach collection="items" item="item" open="(" close=")" separator="UNION ALL">
SELECT #{item.val} FROM DUAL
</foreach>
</select>