【发布时间】:2015-01-19 15:51:49
【问题描述】:
我正在使用 JPA/EclipseLink 2.5.2 从 SQLite 数据库加载两个数据库表。有两张表:一张名为 Dwellings,带有 PK 住宅 ID 和一些其他字段,另一张名为 Households,带有 PK 住宅 ID、FK 住宅 ID 和一些其他字段。
持久性类看起来像这样:
@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 列上放置了一个索引。
我还能尝试什么来加快关联的查找速度?
【问题讨论】:
-
这是一个类似的问题:stackoverflow.com/questions/13400285/…
标签: java sqlite jpa eclipselink