ITPub博客

首页 > 应用开发 > Java > JDBC优化之Batch、Fetch

JDBC优化之Batch、Fetch

Java 作者:1272981665 时间:2015-12-23 22:14:27 0 删除 编辑
Batch和Fetch两个特性非常重要。
Batch相当于JDBC的写缓冲,Fetch相当于读缓冲。

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;

  8. public class Test {
  9.     private static int _1W = 10000;
  10.     private static List<String> list = new ArrayList<String>(100 * _1W);
  11.     static {
  12.         for (int i = 0; i < 10 * _1W; i++) {
  13.             list.add(String.valueOf(i));
  14.         }
  15.     }

  16.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  17.         long start = System.currentTimeMillis();
  18.         fetchRead();
  19.         long end = System.currentTimeMillis();
  20.         System.out.println((end - start) + "ms");
  21.     }

  22.     public static void batchWrite() throws SQLException, ClassNotFoundException {
  23.         // 1108ms
  24.         Class.forName("oracle.jdbc.OracleDriver");
  25.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx");
  26.         connection.setAutoCommit(false);
  27.         PreparedStatement cmd = connection.prepareStatement("insert into t values(?)");
  28.         for (int i = 0; i < list.size(); i++) {
  29.             cmd.setString(1, list.get(i));
  30.             cmd.addBatch();
  31.             if (i % _1W == 0) {
  32.                 cmd.executeBatch();
  33.             }
  34.         }
  35.         cmd.executeBatch();
  36.         connection.commit();
  37.     }

  38.     public static void jdbcWrite() throws ClassNotFoundException, SQLException {
  39.         // 28189ms
  40.         Class.forName("oracle.jdbc.OracleDriver");
  41.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx");
  42.         connection.setAutoCommit(false);
  43.         PreparedStatement cmd = connection.prepareStatement("insert into t values(?)");
  44.         for (String s : list) {
  45.             cmd.setString(1, s);
  46.             cmd.execute();
  47.         }
  48.         connection.commit();
  49.     }

  50.     public static void jdbcRead() throws ClassNotFoundException, SQLException {
  51.         // 3120ms
  52.         Class.forName("oracle.jdbc.OracleDriver");
  53.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx");
  54.         connection.setAutoCommit(false);
  55.         PreparedStatement cmd = connection.prepareStatement("select * from t");
  56.         ResultSet rs = cmd.executeQuery();
  57.         int i = 0;
  58.         while (rs.next()) {
  59.             rs.getString(1);
  60.             i = i + 1;
  61.         }
  62.         System.out.println("count:" + i);
  63.     }

  64.     public static void fetchRead() throws ClassNotFoundException, SQLException {
  65.         //764ms
  66.         Class.forName("oracle.jdbc.OracleDriver");
  67.         Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx");
  68.         connection.setAutoCommit(false);
  69.         PreparedStatement cmd = connection.prepareStatement("select * from t");
  70.         cmd.setFetchSize(_1W);
  71.         ResultSet rs = cmd.executeQuery();
  72.         int i = 0;
  73.         while (rs.next()) {
  74.             rs.getString(1);
  75.             i = i + 1;
  76.         }
  77.         System.out.println("count:" + i);
  78.     }
  79. }
JDBC插入比较,相差25倍
    不带Batch    28189ms
    Batch           1108ms 

JDBC读取比较,相差4倍
    不带Fetch    3120ms
    Fetch           764ms

如果把JDBC类比为JAVA IO的话,
不使用Fetch和Batch相当于直接使用FileInputStream和FileOutputStream
而设置了Fetch和Batch相当于使用BufferedInputStream和BufferedOutputStream

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30649416/viewspace-1878941/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2015-12-21

  • 博文量
    1
  • 访问量
    2451