简体   繁体   English

性能OpenJPA查询(3000+记录)很慢

[英]Performance OpenJPA query (3000+ records) is slow

I'm using Websphere Application Server 7 with buildin OpenJPA 1.2.3 and an Oracle database. 我正在使用带有buildin OpenJPA 1.2.3和Oracle数据库的Websphere Application Server 7。 I have the following entity: 我有以下实体:

    @NamedNativeQuery(name=Contract.GIVE_ALL_CONTRACTS, 
        query="SELECT number, name \n" +
          "FROM contracts \n" +
          "WHERE startdate <= ?1 \n" +
          "AND enddate > ?1",
          resultSetMapping = Contract.GIVE_ALL_CONTRACTS_MAPPING)
    @SqlResultSetMapping(name = Contract.GIVE_ALL_CONTRACTS_MAPPING, 
        entities = { @EntityResult(entityClass = Contract.class, fields = {
          @FieldResult(name = "number", column = "number"),
          @FieldResult(name = "name", column = "name")
        })
    })
    @Entity
    public class Contract {
      public static final String GIVE_ALL_CONTRACTS = "Contract.giveAllContracts";
      public static final String GIVE_ALL_CONTRACTS_MAPPING = "Contract.giveAllContractsMapping";

      @Id
      private Integer number;
      private String name;

      public Integer getNumber() {
        return number;
      }
      public String getName() {
        return name;
      }
    }

And the following code to retrieve the contracts: 以下代码检索合同:

Query query = entityManager.createNamedQuery(Contract.GIVE_ALL_CONTRACTS);
query.setParameter(1, referenceDate);

List contracts = query.getResultList();
entityManager.clear();

return contracts;

The retrieved contracts are passed to a webservice. 检索到的合同将传递给Web服务。

Executing this query in Oracle developer takes around 0,35 seconds for 3608 records. 在Oracle开发人员中执行此查询大约需要0.35秒才能获得3608条记录。 The call to query.getResultList() takes around 4 seconds. 对query.getResultList()的调用大约需要4秒。

With a logger in the constuctor of the entity, it logs that there are about 10-20 entities created with the same timestamp. 使用实体的构造函数中的记录器,它记录了使用相同时间戳创建的大约10-20个实体。 Then 0,015 seconds it does something else. 然后0,015秒它做了别的事情。 I guess OpenJPA stuff. 我想OpenJPA的东西。

Is there a way to speed up OpenJPA? 有没有办法加快OpenJPA? Or is the only solution caching? 或者是唯一的缓存解决方案?

Object creation may have its fair share in the performance hit. 对象创建可能在性能影响方面有其公平的份额。 While running your code in the server, you're not only querying the database but also you allocate memory and create a new Contract object for each row. 在服务器中运行代码时,您不仅要查询数据库,还要为每行分配内存并创建新的Contract对象。 An expanding heap or garbage collection cycle may count for idle periods that you observed. 扩展堆或垃圾回收周期可能会计入您观察到的空闲时段。

I'd suggest you skim through OpenJPA documentation on how to process large results sets . 我建议您浏览一下OpenJPA文档,了解如何处理大型结果集

I suggest you downloading VisualVM and set up a profiling for the packages involved. 我建议你下载VisualVM并为所涉及的软件包设置一个分析。 VisualVM can show the time spent in different methods that will sum up to 0.35sec in your case theoretically. VisualVM可以显示在不同方法中花费的时间,理论上可以总计达到0.35秒。 You will be able to analyze the distribution of the total time between your code, OpenJPA and the network IO. 您将能够分析代码,OpenJPA和网络IO之间的总时间分布。 This will help you to identify the bottleneck. 这将帮助您识别瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM