现在的位置: 首页 > 综合 > 正文

oracle jdbc fetchsize取值对性能的影响

2014年07月31日 ⁄ 综合 ⁄ 共 1566字 ⁄ 字号 评论关闭

       通过JDBC取数据时,默认是10条数据取一次,即fetch size为10,如果增大这个数字可以减少客户端与oracle的往返,减少响应时间,网上有建议这个数字不要超过100,要不然对中间件内存消耗大(没有做过实验)。

package com.gg.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FetchSize {
    static final String driver_class  = "oracle.jdbc.driver.OracleDriver";
    static final String connectionURL = "jdbc:oracle:thin:@10.10.29.150:1522:ordb10";
    static final String userID        = "test";
    static final String userPassword  = "test";
    public void runTest(int fetchSize) {
        Connection  con = null;
        Statement   stmt = null;
        ResultSet   rset = null;
        long startTime =System.currentTimeMillis();
        String  query_string = "SELECT * FROM test";//test有5万条记录
        try {
            Class.forName (driver_class).newInstance();
            con = DriverManager.getConnection(connectionURL, userID, userPassword);
            stmt = con.createStatement();
            stmt.setFetchSize(fetchSize);
            rset = stmt.executeQuery (query_string);
            while (rset.next ()) {
                 rset.getString(1);
                 rset.getString(2);
                 rset.getString(3);
            }
            rset.close();
            stmt.close();
            long endTime =System.currentTimeMillis();
            System.out.println("fetchsize为"+fetchSize+"---消耗的时间:"+(endTime-startTime));
        }  catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FetchSize fetchSize = new FetchSize();
        fetchSize.runTest(10);
        fetchSize.runTest(50);
        fetchSize.runTest(100);
        fetchSize.runTest(200);
        fetchSize.runTest(500);
        fetchSize.runTest(1000);
    }
}

结果:

fetchsize为10    ---消耗的时间:22140ms
fetchsize为50    ---消耗的时间:13780ms
fetchsize为100  ---消耗的时间:5110ms
fetchsize为200  ---消耗的时间:3984ms
fetchsize为500  ---消耗的时间:2625ms
fetchsize为1000---消耗的时间:2875ms

抱歉!评论已关闭.