当前位置:Gxlcms > 数据库问题 > mysql数据库批量高速插入

mysql数据库批量高速插入

时间:2021-07-01 10:21:17 帮助过:10人阅读

static void main(String[] args) { Connection conn = getConn(lsqlurl, luser, lpassword); long startTime = System.currentTimeMillis(); try { PreparedStatement pst = conn.prepareStatement("insert into testmy (id,name,age) values (?

,?,?

)"); for (int i = 0; i < 2000; i++) { pst.setInt(1, 3); pst.setString(2, "xx"); pst.setInt(3, 10); pst.addBatch(); } pst.executeBatch(); long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime)/1000+"s"); System.out.println("test sql batch--->2000....."); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

你会发现时间会是30s 左右。


2k行的数据插入就30秒 。
2w行数据插入时间为940秒(约16min)。

2、改动自己主动提交的 batch

public static void main(String[] args) {
        Connection conn = getConn(lsqlurl, luser, lpassword);
        long startTime = System.nanoTime();
        try {
            conn.setAutoCommit(false);
            PreparedStatement pst = conn.prepareStatement("insert into test (id,name,age) values (?,?,?

)"); for (int i = 0; i < 2000; i++) { pst.setInt(1, 3); pst.setString(2, "xx"); pst.setInt(3, 10); pst.addBatch(); } pst.executeBatch(); conn.commit(); long endTime = System.nanoTime(); System.out.println((endTime - startTime)/1000000+"ms"); System.out.println("test sql batch--->2000....."); } catch (SQLException e) { try { conn.rollback();

mysql数据库批量高速插入

标签:except   nal   print   简单   false   时间   not   java   user   

人气教程排行