首页 > 学院 > 开发设计 > 正文

关于executeBatch()的测试

2019-11-18 11:30:42
字体:
来源:转载
供稿:网友

          这些日子研究ECPerf的原码。今天下午,看到数据的载入。他是采用executeBatch实现的。他是批量的执行SQL语句。于是我写了个测试程序来测试executeBatch的性能。
         我采用的是MySQL数据库,插入100000条纪录,分为三部分测试,一是直接插入,二是使用executeBatch,三是批量是用executeBatch,下面试我得源码和测试结果。

  Connection conn = null;
     try {
       Class.forName("org.gjt.mm.mysql.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "qwe123");
       conn.setAutoCommit(false);
       PReparedStatement ps = conn.prepareStatement("insert into tb_test values (?,?)");
       String date = new java.util.Date().toString();
      
       long l = System.currentTimeMillis();
       for(int i=0;i<100000;i++){
         ps.setString(1, "axman");
         ps.setString(2, date);
         ps.executeUpdate();
         conn.commit();
       }
       ps.executeBatch();
       System.out.println(System.currentTimeMillis()-l);
      
      
      
       long l1 = System.currentTimeMillis();
       for(int i=0;i<100000;i++){
         ps.setString(1, "axman");
         ps.setString(2, date);
         ps.addBatch();
         conn.commit();
       }
       ps.executeBatch();
       System.out.println(System.currentTimeMillis()-l1);
      
      
      
      
       int a[] = new int[100];
       Date  b[] = new Date[100];
       int j=0;
       long l2 = System.currentTimeMillis();
       for(int i=0;i<100000;i++){


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表