JPA在spring-boot项目中“批量插入”非常慢

时间:2014-11-19 14:32:03

标签: java jpa spring-boot batch-insert

嘿我试图插入约800行@oneToMany关系,但看起来它们真的很慢。我不太明白为什么,因为我已经阅读了一些说它应该相当快的指南。
所以我希望某个善良的灵魂可以告诉我是否有一些我误解的东西,可以帮助我提高插入的表现。

存储库:

@Repository
public interface FootnoteRepository extends CrudRepository<FootnotesEntity, Long> {

    @Query("FROM FOOTNOTES WHERE number =?1 AND FOOTNOTE_TYPE=?2 order by DATE_START desc,id asc")
    public List<FootnotesEntity> findFootnoteByNumberAndType(Long number, String transportNumber, Pageable pageable);
}

DomainClass:

@Autowired
private EntityRepository entityRepository;

@Autowired
private FootnoteRepository footnoteRepository;
/**
 * Handles the JPA Interface
 * 
 * @param
 * @return the success of the operation
 */
@Transactional
private boolean insertUpdateFootnoteEntities(List<FootnotesEntity> footnotes) {
    boolean success = true;
    System.out.println("footnotes: " + footnotes.size());
    long start = System.currentTimeMillis();
    try {
        // TODO fix below: (does not "commit" the deletion")
        // footnoteRepository.deleteAll();

        // TODO speed optimize
        footnoteRepository.save(footnotes);

    } catch (Exception e) {
        e.printStackTrace();
        success = false;
    }
    long end = System.currentTimeMillis();
    System.out.println("time: " + (end - start));
    return success;
}

对于本课程,我还尝试添加batchsize,并使用非特定的存储库(基本上是一个EntityManager)的方法:.persist(entity)

&#39;父&#39;实体类:

@Table(indexes = { @Index(columnList = "FOOTNOTE_NUMBER,FOOTNOTE_TYPE") }, uniqueConstraints = @UniqueConstraint(columnNames = {
        "FOOTNOTE_NUMBER", "FOOTNOTE_TYPE", "DATE_START" }))
@Entity(name = "FOOTNOTES")
@EqualsAndHashCode(callSuper = false)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class FootnotesEntity extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "FOOTNOTES_ID")
    protected Long id;

    @Column(name = "FOOTNOTE_NUMBER")
    protected Long number;

    @Column(name = "FOOTNOTE_TYPE")
    protected String footnoteType;

    @Column(name = "APPLICATION_CODE")
    private String applicationCode;

    @Column(name = "shortDescription", length = 2000)
    private String shortDescription;

    @Column(name = "DATE_START")
    private Date startDate;

    @Column(name = "DATE_END")
    private Date endDate;

    @OneToMany(mappedBy = "footnote", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("startDate desc")
    protected List<DescriptionPeriodsEntity> descriptionPeriods;
}

&#39;儿童&#39;实体类:

@Entity(name = "DESCRIPTION_PERIODS")
@EqualsAndHashCode(callSuper = false)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DescriptionPeriodsEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "DESCRIPTION_PERIODS_ID")
    private Long id;

    @Column(name = "DATE_START")
    private Date startDate;

    @Column(name = "DATE_END")
    private Date endDate;

    @Column(name = "DESCRIPTION", length = 5000)
    protected String description;

    @Column(name = "LANGUAGES_ID")
    protected String languagesId;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "FOOTNOTES_ID")
    protected FootnotesEntity footnote;

}

当前运行时: 超过4分钟(276642ms)进行插入(796个脚注行和900个描述字节)

0 个答案:

没有答案