【发布时间】:2017-12-29 15:57:22
【问题描述】:
我目前正在尝试批量插入许多记录(~2000),而 Jooq 的 batchInsert 没有做我想要的。
我正在将 POJO 转换为 UpdatableRecords,然后我正在执行 batchInsert,它正在为每条记录执行插入操作。因此,Jooq 为每个批量插入执行了大约 2000 次查询,这正在扼杀数据库性能。
它正在执行这段代码(jooq 的批量插入):
for (int i = 0; i < records.length; i++) {
Configuration previous = ((AttachableInternal) records[i]).configuration();
try {
records[i].attach(local);
executeAction(i);
}
catch (QueryCollectorSignal e) {
Query query = e.getQuery();
String sql = e.getSQL();
// Aggregate executable queries by identical SQL
if (query.isExecutable()) {
List<Query> list = queries.get(sql);
if (list == null) {
list = new ArrayList<Query>();
queries.put(sql, list);
}
list.add(query);
}
}
finally {
records[i].attach(previous);
}
}
我可以这样做(因为 Jooq 在内部做同样的事情):
records.forEach(UpdatableRecord::insert);
代替:
jooq.batchInsert(records).execute();
如何告诉 Jooq 以批处理模式创建新记录?我应该将记录转换为绑定查询然后调用batchInsert 吗?有任何想法吗? ;)
【问题讨论】:
标签: java database postgresql bulkinsert jooq