我使用JPA / EclipseLink 2.5.2从SQLite数据库加载两个数据库表。有两个表:一个名为Dwellings,有PK住宅和其他一些领域,一个名为带有PK householdId的家庭,一个FK dwelllingId和其他一些领域。
持久性类看起来像这样:
@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列上放了一个索引。
我还能尝试加速查找关联吗?