부분범위 처리

pivate void execute(Connection con) throws Exception {
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT NAME FROM TBL");

    for(int i = 0; i < 100; ++i) {
        if(rs.next()) System.out.println(rs.getString(1));
    }

    rs.close();
    stmt.close();
}

정렬 조건이 있을 때 부분범위 처리

Array Size 조정을 통한 Fetch Call 최소화

부분범위 처리 구현

/** 
 * 부분범위 처리 X
*/
public class AllRange {
    public static void execute(Connection con) throws Exception {
        int arraySize = 10;
        String SQLStmt = "SELECT ID, NAME FROM TBL";
        Statement stmt = con.createStatement();
        stmt.setFetchSize(arraySize);
        ResultSet rs = stmt.executeQuery(SQLStmt);
        while(rs.next()) {
            System.out.println(rs.getLong(1) + " " + rs.getString(2));
        }

        rs.close();
        stmt.close();
    }

    public static void main(String[] args) throws Exception {
        Connection con = getConnection();
        execute(con);
        releaseConnection(con);
    }
}

/** 
 * 부분범위 처리 O
*/
public class PartitialRange {
    public static int fetch(Resultset rs, int arraySize) throws Exception {
        int i = 0;
        whlie(rs.next()) {
            System.out.println(rs.getLong(1) + " " + rs.getString(2));
            if(++i >= arraySize) return i; // 출력 레코드 수가 Array Size에 도달하면 멈춤
        }
        return i;
    }

    public static void execute(Connection con) throws Exception {
        int arraySize = 10;
        String SQLStmt = "SELECT ID, NAME FROM TBL";
        Statement stmt = con.createStatement();
        stmt.setFetchSize(arraySize);
        ResultSet rs = stmt.executeQuery(SQLStmt);
        
        // 사용자 요청이 있을 때 다시 데이터를 Fetch
        while(true) {
            int r = fetch(rs, arraySize);
            if(r < arraySize) break;
            System.out.println("Enter to Continue... (Q)uit?");
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String input = in.readLine();
            if(input.equals("Q")) break;

        }

        rs.close();
        stmt.close();
    }

    public static void main(String[] args) throws Exception {
        Connection con = getConnection();
        execute(con);
        releaseConnection(con);
    }
}

OLTP 환경에서 부분범위 처리에 의한 성능개선 원리

멈출 수 있어야 의미있는 부분범위 처리