Sunday
Nov102013

Performance Testing Hibernate Query Approaches

I've known entities are expensive, but wanted to see for myself, so I built this test project to run some benchmarks. The tests aren't complete - just a simple query with no joins, but with a lot of data. The point was to see how much overhead is introduced after we have the query results.

About the Project

This is a simple Maven Spring project that creates an in-memory HSQLDB database, populates it 500,000 records, and then uses several Hibernate query strategies to fetch every one, and report on their average execution times.

Approaches Tested

  1. Using a JpaRepository interface's findAll method to return a list of attached Hibernate entities.

  2. Using Hibernate's StatelessSession interface to return a list of detached Hibernate entities.

  3. Selecting the specific fields of the entity, using Hibernate to return a simple List, and then manually converting that list to a list of detached entities (as DTOs, basically).

  4. Selecting the specific fields of the entity, then using Hibernate's AliasToBeanResultTransformer to build a list of detached entities (as DTOs, basically).

Changing Execution Parameters

By default, the database is loaded with 500,000 records, and each test is repeated in its own transaction 10 times. You can change both of these values in src/main/resources/application.properties.

Running Tests

This big of a database does take up over 256MB of memory, so you might have to increase your heap space. If you run the tests from Maven, you should be fine, since I increase it in the plugin's settings.

Download the sample project and run the following command from inside its directory:

mvn clean test

The test might take several minutes to run. At the end, the test will output the results.

My Results

The results are listed slowest to fastest:

  ---------------------------
  Testing JpaRepository query
  Total # of runs: 10
  JpaRepository avg time: 1073.5ms

  ---------------------------
  Testing stateless session query
  Total # of runs: 10
  Stateless Session avg time: 818.5ms

  ---------------------------
  Testing RowData query
  Total # of runs: 10
  RowData avg time: 317.7ms

  ---------------------------
  Testing ResultTransformer query
  Total # of runs: 10
  ResultTransformer avg time: 311.9ms

The individual times will vary on different systems - the relative performance is what's important.

The StatelessSession query was a little more efficient than returning attached entities, but still pretty slow for this big query. The AliasToBeanResultTransformer and my custom List -> DTO approaches tied as the best performers. I was hoping for this result, but worried that the reflective nature of AliasToBeanResultTransformer might have introduced some overhead. It did not.

Problems? Let me know!

I tried being as careful as I could with these tests:

  • took the average of several test runs
  • turned off Hibernate's second-level cache
  • turned off Hibernate's query cache
  • cleared the entity manager before each run
  • ran each test in its own transaction
  • ignored the first query in the transaction as to avoid any initial performance hit from opening it

I encourage you to download the project, take a look at the code, and try it out for yourself. If you see any issues with my methodology, please let me know, and I'll correct for it.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (170)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: official source
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: official source
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: xnxx
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: GTA 5 Online Hack
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: camaro burnout
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Vigrax
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: certified pmp
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: here
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Pelatihan SDM is a network marketing and training information or training an employee who has worked with many consulting firms and training institutions.
  • Response
    Response: promo
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: BB8 Toy
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: seo phuket
    Performance testing hibernate query approaches is great blog. Many People have recognized articles but want to see their self so they built this analysis development to scamper some standard and SEO (searching engines optimization) is also use for this purpose.
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: look at here
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: GoPro Pole Review
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: GoPro Selfie Stick
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: http://www.iesa.co
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: talking to
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: similar website
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: lovefind.org
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: epiqly.com
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Kursus Pemrograman
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Go Pro Remote
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: pop over here
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: togel
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: wiki.Econosis.org
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: GoPro Selfie Stick
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: cara daftar sbobet
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: p109343
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Full File
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: bandar euro 2016
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: agen bola euro
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: domino online
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: website
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: agen tangkasnet
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: game judi online
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: agen bola euro
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: edu
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: url
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: bola tangkas asia
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: obrolan bola
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: SEO Company
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: kredyt we frankach
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
  • Response
    Response: igrigonki.net
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: dailystrength.org
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: wordpress blog
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: server resources
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: vps hosting india
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: BHPH Note Buyer
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: kisi-hor-banereh
    kisi-hor-banereh
  • Response
    Best banner design recommendation ever
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: ثبت برند
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Daily Deal Sites
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: jasa seo jakarta
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: medline uniforms
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: medical uniforms
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Syma rc Helicopter
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: vitiligo medicine
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: www.bookmerken.de
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: nonton bokep
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: prediksi online
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: agen online
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: agen casino
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: website
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: cs.cruisebase.com
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: suka
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: Casino Online
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query Approaches
  • Response
    Response: Agen bandarq
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Wow great posts, thanks.
  • Response
    Response: Pratogross.Com
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: twat
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: Togel Singapore
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: more helpful hints
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: Judi online sbobet
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query
  • Response
    Response: Internet Page
    Blake Caldwell - /dev/blake/blog - Performance Testing Hibernate Query

Reader Comments (1)

looks to me as if your tests

"Testing ResultTransformer query" and " Testing RowData query" are the same. That wasn't the intention in the first place I guess.

both tests use the exact same line of code:
allCustomers = customerEntityDao.fetchAllCustomerEntitiesWithAliasToBeanResultTransformer();

probable should be more like:
"Testing RowData query" should be using customerEntityDao.fetchAllCustomerEntitiesWithTableData();

November 18, 2014 | Unregistered CommenterJonathan

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« Solving a 2014 Google I/O Secret Invite Puzzle | Main | Exercising Caution With Hibernate Entities »