【问题标题】:MySQL Batch Inserts Inserting Very SlowlyMySQL批量插入插入非常慢
【发布时间】:2016-02-14 07:34:27
【问题描述】:
try (BufferedReader br = new BufferedReader(new FileReader("LoadData.txt"))){
        final Connection connection = DriverManager.getConnection("jdbc:mysql://ipAddress:port/database?user=username&password=password&useServerPrepStmts=false&rewriteBatchedStatements=true");
        String sCurrentLine;
        final PreparedStatement ps = connection.prepareStatement("insert ignore into emails (email) value (?)");
        int count = 0;
        final int batchSize = 1000;
        while ((sCurrentLine = br.readLine()) != null) {
            ps.setString(1, sCurrentLine);
            ps.addBatch();
            if(++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch();
        ps.close();
        connection.close();
    } catch (final IOException e) {
        e.printStackTrace();
}

即使使用上面的代码,批量插入也非常慢,每 1000 行需要几分钟。有谁知道怎么回事?

【问题讨论】:

  • 请提供SHOW CREATE TABLE 和`SHOW VARIABLES LIKE '%buffer%'。有几件事需要检查(引擎、缓存、数据类型、索引等)。

标签: java mysql sql database performance


【解决方案1】:

发布答案以防将来与其他人混淆:

我的 SQL 查询有关键字“value”。关键字必须是“values”才能使批量插入起作用。

【讨论】:

  • 不,VALUEVALUES (大部分)是同义词。我刚刚测试了一下,用VALUE插入多行没有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2011-08-07
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 2017-01-01
  • 2014-02-27
相关资源
最近更新 更多