응용개발

jdbc에서 질의 플랜정보 보는 방법

by 손승일 posted Apr 15, 2009
JDBC 프로그램에서 질의 플랜정보 보는 방법

CUBRID는  JDBC 프로그램에서 질의 플랜을 출력해주는 메소드를 제공한다.
cubrid.conf 설정

플랜이 출력되려면 CUBRID 설정파일인 $CUBRID/conf/cubrid.conf에 optimization_level=513을 추가하고 DB 서버를 재시작하여야 한다.

# Enable Java Stored Procedure
java_stored_procedure=no
optimization_level=513

간단 예제
다음은 질의 플랜을 화면에 출력하는 간단 예제이다.

import java.sql.*;
import cubrid.jdbc.driver.*; //CUBRIDStatement를 사용하기 위해 import 해야 함.
public class getQueryplan{
        public static void main(String arg[]) throws Exception {
                Connection        conn = null;
                ResultSet         rs = null;
                PreparedStatement  pstmt = null;               
                try {
                        Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
                        conn = DriverManager.getConnection("jdbc:cubrid:localhost:53000:demodb:::","","");
                        conn.setAutoCommit(false); //플랜 정보를 가져오기 위해서는 auto commit을 false로 설정해야 함.
                        String sql = "select host_nation from olympic where host_year = ?";
                        pstmt = conn.prepareStatement(sql);
                        pstmt.setInt(1, 2004);
                        ((CUBRIDStatement)pstmt).setQueryInfo(true);                
                        rs = pstmt.executeQuery();
                        String plan = ((CUBRIDStatement)pstmt).getQueryplan(); // 수행한 질의 플랜 정보를 가져오는 메소드.
                        while(rs.next())
                            System.out.println("host_nation : " + rs.getString(1));
                        conn.commit();
                        System.out.println("plan : " + plan);               
                }catch ( SQLException e ) {
                        e.printStackTrace();
                }catch ( Exception e ) {
                        e.printStackTrace();
                }finally {
                        if (rs != null) rs.close();
                        if (pstmt != null) pstmt.close();
                        if (conn != null) conn.close();
                }
        }
}




Articles

5 6 7 8 9 10 11 12 13 14