[MySql] 다중컬럼 IN

dev asdf·2024년 6월 12일
0

SQL

목록 보기
1/1

되더라. 왜 안되는줄 알고 있었지...ㅋ

MyBatis 사용 시

  • OR 체인
 WHERE (user_id = "abc" AND user_name = "홍길동") 
 OR (user_id = "bcd" AND user_name = "김철수")
 OR ...
  • MyBatis 문법
WHERE 
	<foreach collection = "list" item = "item" separator = "OR">
    	(user_id = #{item.userId} AND user_name = #{item.userName})
    </foreach>

  • 다중컬럼 IN
WHERE (user_id, user_name) IN (("abc","홍길동"),("bcd","김철수))
  • MyBatis 문법
WHERE (user_id, user_name) IN 
	<foreach collection="list" item="item" open="(" separator="," close=")">
    	(#{item.userId}, #{item.userName})
    </foreach>

MyBatis 에서 OR 체인보다 다중컬럼 IN 을 사용하는 것이 속도가 훨씬 빨랐다.
대략 10,000 건의 데이터에 대해 서버 수행속도를 측정해보니 약 500초대 vs 2초 차이로 극심하게 났음


👀 꼭 그런건 아닌듯 한데

https://www.cubrid.com/index.php?mid=faq&category=3794206&document_srl=3794707&listStyle=viewer&page=2

인덱스가 잘 걸려있다면 OR 체인이 더 빠른 경우도 있는 듯 하다.

쿼리 실행 전 EXPLAIN을 통해 적재적소에 알맞은 쿼리를 사용하도록 하자.

끝.

0개의 댓글

관련 채용 정보