0

嘿,我正在尝试插入大约 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);
}

域类:

@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;
}

对于这个类,我还尝试添加批量大小,并使用非特定存储库(基本上是 EntityManager)的方法:.persist(entity)

“父”实体类:

@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;
}

“子”实体类:

@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 分钟(276642 毫秒)进行插入(796 个脚注行和 900 个描述周期)

4

0 回答 0