Real MySQL

Publish date: 2025-06-21
Tags: db 발췌독

http://www.yes24.com/Product/Goods/6960931

대용량데이터 처리를 위한 MySQL 최적화

“useServerPrepStmt=true” 옵션

쓰기 성능 높이기 ( rewriteBatchedStatements=true 옵션)

대용량 데이터 조회를 위한 옵션 파라미터

디폴트옵션들로는 MySQL에서는 JdbcCursorItemReader를 써도 OOM 날 수도 있음. 이를 위한 방법은 디폴트 옵션일때 사용되는 클라이언트사이드 커서는 모든 결과를 다 다운로드함. 이를 해결하기 위해 ResultSet Streaming, 서버 커서 사용할수 있음.

ResultSet Streaming

많은 건의 데이터를 한번에 받지 않고 Streaming으로 흘러보내면서 받는 방법 아래와 같이 Statement를 만들어야함.

PrepaedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE);

‘setFetchSize(Integer.MIN_VALUE)‘은 JDBC규약과도 어긋남. java.sql.Statement의 javadoc에는 setFetchSize로 지정되는 값은 0보다 작으면 SQLException을 던진다고 적혀 있다.

스프링 배치에서도 JdbcCursorItemReader.setFetchSize(Integer.MIN_VALUE) 로 지정하면 쿼리 결과를 한번에 다운로드 받지 않고 Streaming방식으로 가져감.

		return new JdbcCursorItemReaderBuilder<T>()
			.dataSource(this.dataSource)
			.sql(sql)
			.rowMapper(rowMapper)
			.fetchSize(Integer.MIN_VALUE)
			.verifyCursorPosition(false);

ResultSet.TYPE_FORWARD_ONLY 옵션은 JdbcCursorItemReader 에 이미 위의 Statement 생성시의 반영되어 있음.

verifyCursorPosition을 default값인 true로 둘 경우 아래와 같은 에러가 발생함

peration not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.; nested exception is java.sql.SQLException: Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.

서버 커서