[英]What's the effective way to insert more a million rows into postgresql server from another postgres server using Java?
我有两个 postgresql 服务器,我需要从第一个服务器格式复制表行并转换为另一种服务器格式(不同的列名)。
我使用 java 应用程序和 spring 引导和 jpa 存储库,它实现了方法 findAll stream 读取提取大小 1000。
@Query("select c from ExternalFormatEntity c")
@QueryHints(@javax.persistence.QueryHint(name = "org.hibernate.fetchSize",
value = Constants.DEFAULT_FETCH_SIZE))
Stream<ExternalFormatEntity> findAllEntities();
阅读后我批量转换并插入 1000 行。
try (Stream<ExternalFormatEntity> allExtEntitiesStream = extFormatService.getAllEntities()) {
LinkedList<CanonicalFormatEntity> canonicalEntityList = new LinkedList<>();
allExtEntitiesStream.forEach(extEntity -> {
if (Objects.nonNull(extEntity)) {
canonicalEntityList.add(SomeConverter.convert(extEntity));
}
if (canonicalEntityList.size() >= DEFAULT_BATCH_SIZE) {
List<CanonicalFormatEntity> copyList = new LinkedList<>(canonicalEntityList);
canonicalEntityList.clear();
Thread thread = new Thread(() -> {
canonicalEntityRepository.saveAll(copyList);
canonicalEntityRepository.flush();
copyList.clear();
});
thread.start();
}
});
}
在我看来,对于 100 万条记录,此操作的当前速度可以快于 1 小时。 我可以加快这个操作吗,如果可以,该怎么做?
首先,我尝试将第一个数据库的表记录转换为CSV文件,保存在另一台服务器上,使用Postgres Copy Api下载,但由于额外操作硬盘,汇总时间仍然无法接受。
也许 postgres 有 stream 写作或其他东西? 我无法在官方 postgresql 文档中找到答案。
对于我的案例帮助下一个解决方案:
使用 zip 压缩将外部表导出到 csv 文件(来自 StackOverflow 答案的示例: https://stackoverflow.com/a/3981807/3744622 )
将小 zip 文件复制到 /tmp 文件夹中的 postgres 服务器scp root@ext_server:/path/to/file root@target_server:/tmp/
从 csv 压缩文件导入表(来自 StackOverflow 答案的示例: https://stackoverflow.com/a/46228247/3744622 )
我实现了大概10分钟的总结时间。
谢谢大家,这是个好地方)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.