Spring 批量 JdbcBatchItemWriter 插入非常慢 MYSQL
Spring batch JdbcBatchItemWriter insert is very slow with MYSQL
我正在使用 reader 和编写器的块步骤。我正在从 Hive 中读取 50000 块大小的数据,并使用相同的 50000 次提交插入 mysql。
@Bean
public JdbcBatchItemWriter<Identity> writer(DataSource mysqlDataSource) {
return new JdbcBatchItemWriterBuilder<Identity>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql(insertSql)
.dataSource(mysqlDataSource)
.build();
}
当我开始加载数据并插入 Mysql 时,它的提交速度非常慢,加载 100000 条记录需要一个多小时才能加载,而使用 Gemfire 的同一个加载器在 30 分钟内加载了 500 万条记录。
好像它会一个接一个地插入批次,如 laoding 1500 然后 4000 然后 ....等等,有人遇到同样的问题吗?
由于您使用的是 BeanPropertyItemSqlParameterSourceProvider
,这将包括大量反射以在准备好的 statement.This 中设置变量,这将增加时间。
如果速度是您的重中之重,请尝试实施您自己的 ItemWriter
,如下所示,并使用准备好的语句批处理来执行更新。
@Component
public class CustomWriter implements ItemWriter<Identity> {
//your sql statement here
private static final String SQL = "INSERT INTO table_name (column1, column2, column3, ...) VALUES (?,?,?,?);";
@Autowired
private DataSource dataSource;
@Override
public void write(List<? extends Identity> list) throws Exception {
PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(SQL);
for (Identity identity : list) {
// Set the variables
preparedStatement.setInt(1, identity.getMxx());
preparedStatement.setString(2, identity.getMyx());
preparedStatement.setString(3, identity.getMxt());
preparedStatement.setInt(4, identity.getMxt());
// Add it to the batch
preparedStatement.addBatch();
}
int[] count = preparedStatement.executeBatch();
}
}
注:这是粗略的代码。所以异常处理和资源处理没有做好。你可以做同样的事情。我想这会大大提高你的写作速度。
尝试将“;useBulkCopyForBatchInsert=true”添加到您的连接 url。
Connection con = DriverManager.getConnection(connectionUrl + ";useBulkCopyForBatchInsert=true")
我正在使用 reader 和编写器的块步骤。我正在从 Hive 中读取 50000 块大小的数据,并使用相同的 50000 次提交插入 mysql。
@Bean
public JdbcBatchItemWriter<Identity> writer(DataSource mysqlDataSource) {
return new JdbcBatchItemWriterBuilder<Identity>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql(insertSql)
.dataSource(mysqlDataSource)
.build();
}
当我开始加载数据并插入 Mysql 时,它的提交速度非常慢,加载 100000 条记录需要一个多小时才能加载,而使用 Gemfire 的同一个加载器在 30 分钟内加载了 500 万条记录。
好像它会一个接一个地插入批次,如 laoding 1500 然后 4000 然后 ....等等,有人遇到同样的问题吗?
由于您使用的是 BeanPropertyItemSqlParameterSourceProvider
,这将包括大量反射以在准备好的 statement.This 中设置变量,这将增加时间。
如果速度是您的重中之重,请尝试实施您自己的 ItemWriter
,如下所示,并使用准备好的语句批处理来执行更新。
@Component
public class CustomWriter implements ItemWriter<Identity> {
//your sql statement here
private static final String SQL = "INSERT INTO table_name (column1, column2, column3, ...) VALUES (?,?,?,?);";
@Autowired
private DataSource dataSource;
@Override
public void write(List<? extends Identity> list) throws Exception {
PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(SQL);
for (Identity identity : list) {
// Set the variables
preparedStatement.setInt(1, identity.getMxx());
preparedStatement.setString(2, identity.getMyx());
preparedStatement.setString(3, identity.getMxt());
preparedStatement.setInt(4, identity.getMxt());
// Add it to the batch
preparedStatement.addBatch();
}
int[] count = preparedStatement.executeBatch();
}
}
注:这是粗略的代码。所以异常处理和资源处理没有做好。你可以做同样的事情。我想这会大大提高你的写作速度。
尝试将“;useBulkCopyForBatchInsert=true”添加到您的连接 url。
Connection con = DriverManager.getConnection(connectionUrl + ";useBulkCopyForBatchInsert=true")