简体   繁体   English

Hibernate JPA EntityManager.createQuery() 性能

[英]Hibernate JPA EntityManager.createQuery() performance

I have a query which has 2 'in' Clauses.我有一个查询,其中包含 2 个“in”子句。 First in clause takes around 125 values and second in clause of query takes around 21000 values.第一个 in 子句需要大约 125 个值,第二个 in 子句查询需要大约 21000 个值。 Its implemented using JPA CriteriaBuilder .它使用 JPA CriteriaBuilder实现。

Query itself executes very fast and return results within seconds.查询本身执行速度非常快,并在几秒钟内返回结果。 Only problem is entityManager.createQuery(CriteriaQuery) takes around 12-13 minutes to return.唯一的问题是entityManager.createQuery(CriteriaQuery)需要大约 12-13 分钟才能返回。

I search all over SO, all the threads are related to performance of Query.getResultList .Query.getResultList搜索,所有线程都与Query.getResultList性能有关。 None of them discuss about performance of entityManager.createQuery(CriteriaQuery) .他们都没有讨论entityManager.createQuery(CriteriaQuery)性能。 If you have seen such behavior earlier, please let me know, how to resolve it.如果您之前看到过此类行为,请告诉我如何解决。

My JDK version is 1.7.我的 JDK 版本是 1.7。 Dependency version of javaee-api is 6.0. javaee-api 的依赖版本是 6.0。 Application is deployed on JBOSS EAP 6.4.应用程序部署在 JBOSS EAP 6.4 上。 But that's not the concern as of now, as I am testing my code using junit using EntityManager connected to actual Oracle database.但这不是现在的问题,因为我正在使用连接到实际 Oracle 数据库的EntityManager使用 junit 测试我的代码。 If you require more information, kindly let me know.如果您需要更多信息,请告诉我。

A hybrid approach is to dynamically create a query and then save it as a named query in the entity manager factory.混合方法是动态创建查询,然后将其保存为实体管理器工厂中的命名查询。

At that point it becomes just like any other named query that may have been declared statically in metadata.在这一点上,它变得就像可能已在元数据中静态声明的任何其他命名查询一样。 While this may seem like a good compromise, it turns out to be useful in only a few specific cases.虽然这看起来是一个很好的折衷方案,但结果证明它只在少数特定情况下有用。 The main advantage it offers is if there are queries that are not known until runtime, but then reissued repeatedly.它提供的主要优点是,如果存在直到运行时才知道的查询,但随后会重复重新发出。 Once the dynamic query becomes a named query it will only bear the cost of processing once.一旦动态查询成为命名查询,它将只承担一次处理成本。

It is implementation-specific whether that cost is paid when the query is registered as a named query, or deferred until the first time it is executed.是在查询注册为命名查询时支付该费用还是推迟到第一次执行时支付该费用是特定于实现的。

A dynamic query can be turned into a named query by using the动态查询可以通过使用

EntityManagerFactory addNamedQuery()

Keep us informed by the result and good luck让我们知道结果并祝你好运

I observed that, having single query with 21 IN clauses (each with 1000 expressions) and all combined with OR clauses, made query run slower.我观察到,具有 21 个 IN 子句(每个有 1000 个表达式)的单个查询以及所有与 OR 子句相结合的查询,使查询运行速度变慢。 I tried another approach of executing every IN Clause as a part of separate query.我尝试了另一种将每个 IN 子句作为单独查询的一部分执行的方法。 So these 21 individual queries performed better overall.因此,这 21 个单独的查询总体上表现更好。

Another issue I observed was that Query with CriteriaBuilder was slow when result set is huge (something like 20K rows in result set).我观察到的另一个问题是,当结果集很大(结果集中有 20K 行)时,使用 CriteriaBuilder 进行查询很慢。 I solved this issue by adding query hint to my typed query:我通过向键入的查询添加查询提示解决了这个问题:

TypedQuery.setHint("org.hibernate.fetchSize", 5000);

Hope it will help others.希望它能帮助别人。

Code in Hibernate is not expected to be used for binding lots of params: Hibernate 中的代码不应用于绑定大量参数:

        for ( ImplicitParameterBinding implicitParameterBinding : parameterMetadata.implicitParameterBindings() ) {
            implicitParameterBinding.bind( jpaqlQuery );
        }

Unfortunately you need to find different approach if you want to do something similar.不幸的是,如果你想做类似的事情,你需要找到不同的方法。

暂无
暂无

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

相关问题 entityManager.createQuery()上的AbstractMethodError - AbstractMethodError on entityManager.createQuery() entityManager.createQuery()抛出NullPointerException - entityManager.createQuery() throwing NullPointerException java.sql.SQLException: ORA-00942: 表或视图不存在 JPA entityManager.createQuery() - java.sql.SQLException: ORA-00942: table or view does not exist with JPA entityManager.createQuery() entityManager.createQuery() 花费大量时间来构建查询和绑定参数。 性能受到影响 - entityManager.createQuery() taking lot of time to build query and bind the parameters. Performance affected 使用IN的JPA EntityManager createQuery()不起作用 - JPA EntityManager createQuery() with IN not working JPA EntityManager createQuery()错误 - JPA EntityManager createQuery() error 使用 EntityManager.createQuery 在 DAO 实现中制定 sql 查询时遇到问题 - Trouble formulating a sql query in DAO Implementation using EntityManager.createQuery enitityManager.find和entityManager.createQuery有什么区别? - What is the difference between enitityManager.find and entityManager.createQuery? QueryDSL 如何将 ParamExpression 添加到 JPAQuery 以使用 EntityManager.createQuery 执行 - QueryDSL how to add ParamExpression to JPAQuery to be executed with EntityManager.createQuery 从 entityManager.createQuery() 获取特定字段的响应 加入 springboot API 结果 - get response with specific fields from entityManager.createQuery() join result in springboot API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM