Java Hibernate数据库速度问题

时间:2018-08-22 11:23:58

标签: java hibernate spring-data-jpa jhipster

我使用Jhipster生成了应用程序。开始时一切正常,但是随着应用程序的增长,锦标赛实体成为有关表演的问题。 这是我的实体:

/**
 * A Tournament.
 */
@Entity
@Table(name = "tournament")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "tournament")
public class Tournament implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

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

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

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

    @Column(name = "start_date")
    private ZonedDateTime startDate;

    @Column(name = "end_date")
    private ZonedDateTime endDate;

    @Column(name = "entry_fee")
    private Double entryFee;

    @Column(name = "prize")
    private Double prize;

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

    @Column(name = "favorite_rating")
    private Long favoriteRating;

    @Column(name = "participants_number")
    private Integer participantsNumber;

    @Column(name = "finished")
    private Boolean finished;

    @Column(name = "view_only")
    private Boolean viewOnly;

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

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

    @Column(name = "teams_applied")
    private String teamsApplied;

    @Lob
    @Column(name = "schedule")
    private String schedule;

    @Lob
    @Column(name = "prize_distribution")
    private String prizeDistribution;

    @Lob
    @Column(name = "contacts")
    private String contacts;

    @Lob
    @Column(name = "rules")
    private String rules;

    @OneToMany(mappedBy = "tournament", fetch = FetchType.LAZY)
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Stream> streams = new HashSet<>();

    @ManyToMany
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JoinTable(name = "tournament_platforms", joinColumns = @JoinColumn(name = "tournaments_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "platforms_id", referencedColumnName = "id"))
    private Set<Platform> platforms = new HashSet<>();

    @ManyToMany(mappedBy = "favoriteTournaments", fetch = FetchType.LAZY)
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<User> favoriteUsers = new HashSet<>();

    @ManyToOne
    private Game game;

    @ManyToOne
    private TournamentStatus status;

    @ManyToOne
    private EntryType entryType;

    @ManyToOne
    private TournamentFormat format;

    @ManyToOne
    private Region region;

    @ManyToOne
    private GameMode gameMode;

    @ManyToOne
    private PrizeType prizeType;

    @ManyToOne
    private Organizer organizer;

    @ManyToOne
    private TournamentStage stage;

    @ManyToOne
    private HostPlatform hostPlatforms;

    @ManyToOne
    private TournamentType type;

    @ManyToOne
    private PlayType playType;

    @ManyToOne
    private Currency currency;

    @ManyToOne
    private Country country;

我正在使用Spring JPA。从数据库中获取20个锦标赛需要39秒。那是不可接受的。有什么办法可以将其降低到正常速度。如此长的响应时间是什么原因?我变得平淡无奇

1 个答案:

答案 0 :(得分:5)

在hibernate的JPA实现中,默认情况下@ManyToOne有一个fetchType = EAGER,而您有 14

@ManyToOne
private Country country;

这意味着每个请求 14个加入。我强烈建议对所有关系使用fetchType = LAZY,并在需要时逐一停用它们。

根据经验,每个请求不应使用超过3个连接。

还要查看生成的请求,并使用EXPLAIN PLAN来了解数据库的实际功能以及昂贵的成本。它可能会揭示用作外键的列上缺少一些索引...