Home > Database > Mysql Tutorial > oracle jdbc fetchsize取值对性能的影响

oracle jdbc fetchsize取值对性能的影响

WBOY
Release: 2016-06-07 16:35:40
Original
2023 people have browsed it

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

?????? 通过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);
    }
}
Copy after login


结果:

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

作者:guogang83 发表于2013-8-28 17:24:37 原文链接

阅读:7 评论:0 查看评论

oracle jdbc fetchsize取值对性能的影响

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template