我必须使用JPA加载树(文件夹和文件)。但是有超过1000个节点,加载整个树需要14秒。
Categorie.java:
public class Categorie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idcategorie;
@ManyToOne()
@JoinColumn(name = "IDCATPARENT")
private Categorie categorieParent;
@OneToMany(mappedBy = "categorieParent", cascade = ALL)
private List<Categorie> listeCategorie = new ArrayList<Categorie>();
@OneToMany(mappedBy = "categorieParent", cascade = ALL)
private List<Fichier> listeFichier = new ArrayList<Fichier>();
Fichier.java:
public class Fichier implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idfichier;
@ManyToOne()
@JoinColumn(name = "IDCATEGORIE")
private Categorie categorieParent;
数据存储在DB2中,我用来加载树:
SELECT c FROM Categorie c LEFT JOIN FETCH c.listeCategorie
SELECT c FROM Categorie c LEFT JOIN FETCH c.listeFichier
如何加快加载速度?
谢谢,对不起我的英文......
编辑: 由OpenJPA生成的查询
SELECT t0, t1, t2 FROM SCHEMA.Categorie t0 LEFT OUTER JOIN SCHEMA.Categorie t1 ON t0.IDCATPARENT = t1.idcategorie LEFT OUTER JOIN SCHEMA.Categorie t2 ON t0.idcategorie = t2.IDCATPARENT WHERE (t0.supprimer = CAST(? AS BIGINT)) ORDER BY t2.IDCATPARENT ASC
openjpa.jdbc.SQL - <t 1860790064, conn 339897564> [56 ms] spent
SELECT t0, t1, t2 FROM SCHEMA.Categorie t0 LEFT OUTER JOIN SCHEMA.Categorie t1 ON t0.IDCATPARENT = t1.idcategorie LEFT OUTER JOIN SCHEMA.Fichier t2 ON t0.idcategorie = t2.IDCATEGORIE WHERE (t0.supprimer = CAST(? AS BIGINT)) ORDER BY t2.IDCATEGORIE ASC
openjpa.jdbc.SQL - <t 1860790064, conn -1256330574> [210 ms] spent
所以调用数据库的速度非常快,但之后需要10-15秒才能将结果作为列表...