本文介绍了Hibernate Criteria vs HQL:哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读一些歌曲,但我仍然感到困惑。为什么?因为你提到的差异与表现无关。它们与易用性有关(Objetc(条件)和SQL(hql))。但我想知道是否由于某些原因,标准比hql慢。



我在另一个文章中读到这个



在HQL和criteriaQuery之间的性能方面有所不同,每次使用criteriaQuery启动查询时,它会为表名称创建一个新的别名,该名称不会反映在任何DB的上次查询缓存中。到编译生成的SQL的开销,需要更多时间来执行。由Varun Mehta。



这非常接近但是!我在另一个网站上阅读(http://gary-rowe.com/agilestack/tag/hibernate/)Hibernate 3.3及更高版本不再是这种情况(请阅读:9)Hibernate速度很慢,因为SQL生成的SQL Criteria接口是不一致的)



我已经做了一些测试,试图找出差异,但都生成qry,并且不会更改表的别名。 p>

我很困惑。如果有人知道主要原因,请帮助我们。谢谢

解决方案
我是2004年编写Hibernate 3查询翻译器的人,所以我知道它是如何工作的。

理论上来说,HQL查询的开销应该比HQL查询少(除了命名查询,我将会介绍)。这是因为Criteria不需要解析任何东西。使用基于ANTLR的解析器分析HQL查询,然后将生成的AST转换为SQL。但是,使用HQL / JPAQL,您可以定义命名查询,其中在
SessionFactory启动时生成SQL。理论上,命名查询比Criteria的开销少。



因此,就SQL生成开销而言,我们有:


  1. 命名为HQL / JPAQL查询 - SQL生成只发生一次。

  2. 标准 - 生成前无需解析。
  3. (非命名)HQL / JPAQL查询 - 解析,然后生成。

也就是说, 在我看来,选择基于解析和SQL生成开销的查询技术可能是一个错误 。与使用真实数据在真实数据库服务器上执行真实查询相比,这种开销通常非常小。如果这个开销实际上在分析应用程序时显示出来,那么应该切换到一个命名查询。



这是我在考虑什么时候考虑的事情在Criteria和HQL / JPAQL之间做出决定:


  • 首先,您必须决定是否 OK 代码中的依赖Hibernate专有API 。 JPA没有标准。

  • 标准非常擅长处理许多可选的搜索参数,例如您可以在具有多参数的典型网页上找到'搜索表单'。对于HQL,开发人员倾向于使用StringBuilder(避免这种情况!)处理where子句表达式。有了标准,你不需要那样做。 Hardik发表了类似的观点。

  • HQL / JPAQL可用于大多数其他事情,因为代码往往更小,更易于开发人员理解。

  • 如果您使用HQL,则可以将真正频繁的查询转换为命名查询。我希望稍后再进行一些分析。


I have been reading some anwers, but i'm still confused. ¿Why? because the differences that you have mentioned do not relate with the performance. they are related with easy use.(Objetc(criteria) and SQL(hql)). But I would like to know if "criteria" is slower than hql for some reason.

I read this in another anwers

"There is a difference in terms of performance between HQL and criteriaQuery, everytime you fire a query using criteriaQuery, it creates a new alias for the table name which does not reflect in the last queried cache for any DB. This leads to an overhead of compiling the generated SQL, taking more time to execute." by Varun Mehta.

This is very close BUT! i read in another website(http://gary-rowe.com/agilestack/tag/hibernate/) This is no longer the case with Hibernate 3.3 and above(please read this: 9) Hibernate is slow because the SQL generated by the Criteria interface is not consistent)

I have done some test trying to find out the differences but both generate qry's and it doesn't change the alias to the table.

I'm very confused. If somebody knows the main reason please, could you help us. Thanks

解决方案

I'm the guy who wrote the Hibernate 3 query translator back in 2004, so I know something about how it works.

Criteria, in theory should have less overhead than an HQL query (except for named queries, which I'll get to). This is because Criteria doesn't need to parse anything. HQL queries are parsed with an ANTLR-based parser and then the resulting AST is turned into SQL. However, with HQL/JPAQL you can define named queries, where the SQL is generated when theSessionFactory starts up. In theory, named queries have less overhead than Criteria.

So, in terms of SQL-generation overhead we have:

  1. Named HQL/JPAQL Query - SQL generation happens only once.
  2. Criteria - No need to parse before generating.
  3. (non-named) HQL/JPAQL Query - Parse, then generate.

That said, choosing a query technique based on the overhead of parsing and SQL generation is probably a mistake in my opinion. This overhead is typically very small when compared to performing a real query on a real database server with real data. If this overhead does actually show up when profiling the app then maybe you should switch to a named query.

Here are the things I consider when deciding between Criteria and HQL/JPAQL:

  • First, you have to decide if you're OK with having a dependency on Hibernate-proprietary API in your code. JPA doesn't have Criteria.
  • Criteria is really good at handling many optional search parameters such as you might find on a typical web page with a multi-parameter 'search form'. With HQL, developers tend to tack on where clause expressions with StringBuilder (avoid this!). With Criteria, you don't need to do that. Hardik posted similar opinions.
  • HQL/JPAQL can be used for most other things, because the code tends to be smaller and easier for developers to understand.
  • Really frequent queries can be turned into named queries if you use HQL. I prefer to do this later, after some profiling.

这篇关于Hibernate Criteria vs HQL:哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:07