CrudRepository findAll()让位于long - 替代?提高性能?

时间:2018-01-08 00:03:37

标签: java spring spring-boot spring-data

在我的控制器中我使用CrudRepository方法findAll()来查找我的数据库中的所有用户,如下所示:

userRepository.findAll()

问题在于,这样做需要1.3分钟才能加载1.500个用户。从那里我用Thymeleaf在模型中加载数据我在一个html表中显示它:名称,时间创建,电子邮件,id,数据包和每个用户的状态。有没有办法提高性能或解决我的问题? 任何帮助都会非常准确。

这是我的用户实体

    @Id
    @SequenceGenerator(name = "user_id_generator", sequenceName = "user_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_generator")
    private Long id;
    @Column(nullable = false, unique = true)
    private String email;
    @Column(name = "uuid", nullable = false, unique = true)
    private String uuid;
    @Column(name = "reset_pwd_uuid", unique = true)
    private String resetPwdUuid;
    @Column(nullable = false)
    private String password;
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private Status status;
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private Packet packet = Packet.BASE;
    @Enumerated(EnumType.STRING)
    @Column
    private Situation situation;
    @Column(nullable = false, name = "numberOfSomething")
    private Integer apples;
    @Column(nullable = false, name = "numberOfSomethingElse")
    private Integer oranges;
    @Column(name = "time_created")
    private Timestamp timeCreated;

    @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
    @LazyCollection(LazyCollectionOption.FALSE)
    @OrderBy("rank ASC")
    private List<Person> person;

    @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
    @LazyCollection(LazyCollectionOption.FALSE)
    @OrderBy("timeOccured ASC")
    private List<History> history;

    @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
    @LazyCollection(LazyCollectionOption.FALSE)
    @OrderBy("id ASC")
    private List<Invoice> invoices;

    @OneToOne(mappedBy = "user", cascade = {CascadeType.REMOVE})
    private Building building;

    @OneToOne(mappedBy = "user", cascade = {CascadeType.REMOVE})
    private House house;

    @OneToOne(mappedBy = "user", cascade = {CascadeType.REMOVE})
    private Car car;

    @OneToOne(mappedBy = "user", cascade = {CascadeType.REMOVE})
    private Street street;

    @OneToOne(mappedBy = "user", cascade = {CascadeType.REMOVE})
    private Moreof moreof;

    @JoinColumn(name = "basedata_id", referencedColumnName = "id")
    @ManyToOne(cascade = {CascadeType.REMOVE})
    private Basedata basedata;

    @Column(name = "family")
    private String family;

    @Column(name = "unkle_mail")
    private boolean unkleMail;

    @Column(name = "vacation")
    private LocalDate vacationUntil;

    @Column(name = "ordered")
    private boolean ordered;

    @Column(name = "shipped")
    private boolean shipped;

    @Transient
    private boolean isEdit;

    @Transient
    private boolean one;
    @Transient
    private boolean two;
    @Transient
    private boolean three;
    @Transient
    private boolean four;
    @Transient
    private LocalDate regDate;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.REMOVE)
    private List<Bed> bed;

1 个答案:

答案 0 :(得分:5)

加载1500用户不应该花那么长时间,加载关联需要很长时间。

<强>问题

默认情况下,在JPA中,任何toMany关系都是延迟加载的,这意味着您的实体与集合一起拥有代理,并且集合实际上是在第一次访问时加载的,因此在这种情况下,提取1500用户不应该长。

在您的情况下,您通过在jpa级别指定fetch或在hibernate级别使用@LazyCollection(LazyCollectionOption.FALSE)来禁用延迟加载,不建议这样做,并且预计会出现性能问题。

<强>解决方案

显然,解决方案是不禁用延迟加载