Hibernate Criteria vs HQL: which is faster?

PerformanceHibernateHqlHibernate Criteria

Performance Problem Overview


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

Performance Solutions


Solution 1 - Performance

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 the SessionFactory 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.

Solution 2 - Performance

I am working on millions of record. I found HQL is much more faster than Criteria. Criteria lags a lot in performance.

If you are dealing with lot of data then go for HQL.

Solution 3 - Performance

I mostly prefer Criteria Queries for dynamic queries. For example it is much easier to add some ordering dynamically or leave some parts (e.g. restrictions) out depending on some parameter.

On the other hand I'm using HQL for static and complex queries, because it's much easier to understand/read HQL. Also, HQL is a bit more powerful, I think, e.g. for different join types.

https://stackoverflow.com/questions/197474/hibernate-criteria-vs-hql/197496#197496

Solution 4 - Performance

Other Advantages I think for Criteria over HQL:

Writing HQL makes the code messy. It doesn't look like writing clean Object Oriented Code anymore.

While writing Criteria Queries, IDE suggests us with Intellisense, so there is less chance of committing mistakes except when we write something like variable names in double quotes.

Solution 5 - Performance

You are right and not. Result list is retrieved from database or cache with the org.hibernate.loader.Loader class. When cache is not enabled, prepared statement is build using the Dialect object which is created in SessionFactoryImp. So, statements either initialized per list call. Besides, low-level query is generated automatically. Basically, it is an aid, but there may be a case when a specific query will be more effective when manually written.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkcobuntuView Question on Stackoverflow
Solution 1 - PerformanceJoshua DavisView Answer on Stackoverflow
Solution 2 - PerformanceRajeevView Answer on Stackoverflow
Solution 3 - PerformanceHardik MishraView Answer on Stackoverflow
Solution 4 - Performanceomkar sirraView Answer on Stackoverflow
Solution 5 - PerformanceSimonView Answer on Stackoverflow