插入记录时,使用JDBC Batch Update插入100k记录30分钟

时间:2018-03-06 11:03:21

标签: java mysql

我正在使用Java编写一个迁移脚本。我想将数据从SQL Server迁移到Mysql。我使用以下代码迁移1个表,但性能非常慢。迁移10万条记录大约需要30分钟。

private void pushIntoMySqlServer_EmailSubscription(ResultSet rs) {
    try {
        log.info("Updating Data into MYsql DB For Table:  ich_emailsubscription");
        PreparedStatement pst = mySqlCon.prepareStatement(
                "INSERT INTO ich_emailsubscription(SubscriptionId, SubscriptionTime, Email, SubscriberName, Suburb, "
                        + " State, PostCode, url, device) " + "VALUES(?,?,?,?,?,?,?,?,?)"
                        + "ON DUPLICATE KEY UPDATE " + "SubscriptionTime = VALUES(SubscriptionTime),"
                        + "Email = VALUES(Email)," + "SubscriberName = VALUES(SubscriberName),"
                        + "Suburb = VALUES(Suburb)," + "State = VALUES(State)," + "PostCode = VALUES(PostCode),"
                        + "url = VALUES(url)," + "device = VALUES(device)");
        int i = 1;
        int records = 0;
        while (rs.next()) {
            pst.setInt(1, rs.getInt(1));
            pst.setDate(2, rs.getDate(2));
            pst.setString(3, rs.getString(3));
            pst.setString(4, rs.getString(4));
            pst.setString(5, rs.getString(5));
            pst.setString(6, rs.getString(6));
            pst.setString(7, rs.getString(7));
            pst.setString(8, rs.getString(8));
            pst.setString(9, rs.getString(9));
            pst.addBatch();
            if (i % 10000 == 0) {
                pst.clearParameters();
                records += pst.executeBatch().length;
            }
            i++;
        }
        records += pst.executeBatch().length;
        log.info("Data Updated For Table :  ich_emailsubscription : " + records);
        saveLastFetchTime(600, records, 0);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e);
    }
}

0 个答案:

没有答案