JPA OneToMany的Tweek性能

时间:2018-04-14 16:48:26

标签: java mysql performance hibernate jpa

我有以下JPA实体:

实体

@Entity
@Table(name = "todo")
@NamedEntityGraph(name = "TodoEntity.children",
        attributeNodes = @NamedAttributeNode("children"))
public class TodoEntity {

    public TodoEntity() {
    }

    @Id
    @GeneratedValue
    private Long id;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Long userId;

    private String text;

    private String description;

    private Boolean collapsed;

    private Boolean completed;

    private Integer sortOrder;

    private Date expiredDate;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;

    private Integer priority;

    private String guid;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @JsonIgnore
    @Column(updatable = false,nullable = false)
    private Long parentId;

    @OneToMany(fetch = FetchType.EAGER)
    @Cascade({CascadeType.ALL})
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "parentId", referencedColumnName = "id")
    @OrderBy("sortOrder ASC")
    private List<TodoEntity> children;
}

回购

@EntityGraph(value = "TodoEntity.children", type = EntityGraph.EntityGraphType.FETCH)
    Page<TodoEntity> findByUserIdAndParentId(Long userId, Long parentId, Pageable pageable);

我希望有一个可以无限嵌套的TodoList。 到目前为止代码工作 - 唯一的事情是,当我有大约1500个待办事项并且它们是嵌套的时,SQL和查询变得缓慢(大约1,3s)

我该如何改善这个?

IDEA:

实际上我认为用一个查询(需要花费2,2毫秒)然后将它们与java(拥有parentId)嵌套在数据库中的所有Todos就足够了,以便工作负载在应用层上。

0 个答案:

没有答案