Apache camel 驼峰SQL脚本的性能比使用JPA迁移数据库的脚本差
我们有一个驼峰集成路线,有两个不同的解决方案,一个使用SQL,另一个使用JPA 使用SQL的原始脚本:Apache camel 驼峰SQL脚本的性能比使用JPA迁移数据库的脚本差,apache-camel,Apache Camel,我们有一个驼峰集成路线,有两个不同的解决方案,一个使用SQL,另一个使用JPA 使用SQL的原始脚本: @Component public class PersonRoute extends RouteBuilder { private final PersonRepository personRepository; public PersonRoute(PersonRepository personRepository) { this.personRepos
@Component
public class PersonRoute extends RouteBuilder {
private final PersonRepository personRepository;
public PersonRoute(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
public void configure() {
final JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
json.setUnmarshalType(PersonEntity.class);
from("sql:SELECT * FROM `my_table`?repeatCount=1%26dataSource=#importDataSource")
.autoStartup("{{startupMyRoute}}")
.threads(10)
.to("sql:"
+ "INSERT INTO my_table (`id`, `name`, `age`) "
+ "values(:#${body[ID]},:#${body[NAME]},:#${body[AGE]})"
+ "?dataSource=#jpaDataSource");
}
}
使用JPA的解决方案:
@Component
public class PersonRoute extends RouteBuilder {
private final PersonRepository personRepository;
public PersonRoute(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
@SuppressWarnings("unchecked")
public void configure() {
JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
json.setUnmarshalType(PersonEntity.class);
from("sql:SELECT" +
" JSON_MERGE(" +
" '{}'," +
" JSON_OBJECT(" +
" 'id', `person`.`id`," +
" 'name', `person`.`name`," +
" 'age', `person`.`age`" +
" )" +
" )" +
"FROM `person`?repeatCount=1%26dataSource=#importDataSource")
.routeId("PersonRoute")
.autoStartup("{{startupPersonRoute}}")
.threads(10)
.process(
xchg -> {
Map<String, Object> resultMap = (Map) xchg.getIn().getBody();
xchg.getIn()
.setBody(resultMap.entrySet().stream().findFirst().map(Entry::getValue).orElse(null),String.class);
})
.unmarshal(json)
// Aggregate until we have 1200 records OR 15 seconds have elapsed:
.aggregate(constant(true), new GroupedBodyAggregationStrategy())
.completionSize(1200)
.completionInterval(15000)
.parallelProcessing()
.process(
xchg ->
personRepository.saveAll(
(List<PersonEntity>) xchg.getIn().getBody()))
.end();
}
}
迁移500k行时,我们得到以下结果
SQL解决方案2m16s
JPA解决方案1m36s
我知道这看起来并没有什么大的区别,但对于具有许多外键和约束的真实实体,这种差异会持续3到6个小时
我认为JPA没有理由比SQL表现得更好,所以我相信我缺少了一些可以让我运行得更快的配置
JPA应该比SQL更快吗?若然,原因为何?如果没有,我在这里遗漏了什么?性能差异的原因之一是,JPA方法是聚合记录并将记录保存在一批1200条记录中,或者在一个事务等待15秒后保存 下面是一篇关于saveAll如何比保存单个记录更快的帖子- 如果JPA代码中没有聚合,并且您一次保存一条记录,那么性能可能是相同的