通过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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐