我正在使用 JPA/EclipseLink 2.5.2 从 SQLite 数据库加载两个数据库表。有两张表:一张名为 Dwellings,具有 PK housingId 和一些其他字段,一张名为 Households,具有 PK familyId、FK housingId 和一些其他字段。
持久化类看起来像这样:
@ReadOnly
@Entity
@Table(name="Dwellings")
@NamedQuery(name = "Dwelling.findAll", query = "SELECT d FROM Dwelling d")
public class Dwelling implements Serializable {
private static final long serialVersionUID = -2430404920797112159L;
@Id
private int dwellingId;
@OneToOne(mappedBy="dwelling", optional=true, cascade=CascadeType.ALL)
private Household household;
(...)
}
和
@ReadOnly
@Entity
@Table(name="Households")
@NamedQuery(name="Household.findAll", query="SELECT h FROM Household h")
public class Household implements Serializable {
private static final long serialVersionUID = -360339872479840811L;
@Id
private int householdId;
@OneToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "DwellingId")
private Dwelling dwelling;
(...)
}
现在,如果我在未启用关联的情况下加载包含约 900,000 行的表格住宅(使用下面的命令),整个加载将花费大约 10 秒。
TypedQuery<Dwelling> dwellingQuery = entitymanager.createQuery("select d from Dwelling d", Dwelling.class);
List<Dwelling> dwellings = dwellingQuery.getResultList();
如果我启用两个表之间的关联(包含约 800.000 行的家庭),加载将需要很长时间(我在约 10 分钟后中断了它)。
我在 Households.dwellingId 列上放置了一个索引。
我还可以尝试什么来加快关联查找速度?
最佳答案
也许它与 ORM n+1 performance problem. 相关
关于java - 使用关联时,使用 eclipselink/jpa 加载大型数据库表需要非常长的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028516/