JPA TypedQuery getResultList()性能非常慢

时间:2015-07-28 06:45:41

标签: java oracle performance jpa criteriaquery

我的代码如下:

final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final TypedQuery<ObjectTO> createQuery = criteriaBuilder.createQuery(ObjectTO.class);
...
// Logic to create search query
...
// On this line performance is very low. Takes around 200 seconds for 3000 records.
final List<ObjectTO> inhibits = createQuery.getResultList();

生成的SQL(select子句中减少列的示例)如下所示:

SELECT t1.COL1 ,
    t1.COL2 ,
    t1.COL3 ,
    t2.COL1 ,
    t2.COL2 ,
    t3.COL2 
  FROM Table1 t1
  INNER JOIN Table2 t2,
  ON t1.COL1 = t2.COL1
  LEFT OUTER JOIN Table3 t3
  ON t1.COL1 = t3.COL1
  WHERE (t1.COL2 LIKE 'test')
  AND ( t1.COL3 >= SYSDATE
  OR t1.COL3 = to_Date('1901.01.01 00:00:00', 'yyyy-mm-dd HH24:mi:ss') )
  ORDER BY t1.COL1 ASC ,
    t1.COL3 ASC ,
    t2.COL2 ASC;

在SQL开发人员执行时查询相同,大约需要0.3秒。请注意,如果使用java.sql.Statement.executeQuery()执行相同的查询,则需要大约0.4秒才能从数据库中获取数据。请注意,数据库是Oracle 11g。 JDK版本是1.7.0_51。

Table1的JPA实体如下所示:

@Entity
@Table(name = "Table1")
public class Table1 implements Serializable {
    private static final long     serialVersionUID = 1L;

    @Id
    @Column(name = "COL1", insertable = false, updatable = false)
    private String                col1;

    @Column(name = "COL3", insertable = false, updatable = false)
    private Timestamp             col3;

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

    @OneToMany(mappedBy = "col1", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    private Set<Table2> table2;

    @OneToMany(mappedBy = "col1", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    private Set<Table3>  table3;

    ...
    ...
}

您能否提供一些提示,告诉我在这种情况下我应该从哪里开始寻求提高性能?如果您需要更多信息,请告诉我。

0 个答案:

没有答案