• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

oracle jdbc fetchsize取值对性能的影响

mysql 搞代码 4年前 (2022-01-09) 52次浏览 已收录 0个评论

?????? 通过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, userPasswo<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong>rd);            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

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

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:oracle jdbc fetchsize取值对性能的影响
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址