-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 10:50 am 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
Hi , i just built an application with Hibernate, which use an MySql database.

I have a snippet of code like below :
Code:
                       List<FitArticle> articleList = new ArrayList<>();
            PerformanceTest.setStarttime();
            articleList = db.getArticleByFilter(Filter.STANDARD);
            PerformanceTest.printTime("Getting Articles");
            
            PerformanceTest.setStarttime();
            List<FitArticle> articleNewList = new ArrayList<>();
            articleNewList.add(articleList.get(0));
            
            System.out.println(articleList.size());
            controller.renderPage(articleList, page, firstCall);                  
            PerformanceTest.printTime("Rendering");



By Using PerformanceTest which just mesures the time between, i got a result that Fetching a list of objects (300 items) take only 200ms or something like that. But working with these new fetched objects take way way longer than it used to be with JDBC.

For clarity, by working with objects i meant render the objects to a html web view.

Further more, i tried to troubleshooting the problem, by using YourKit Java Profiler to figure out exactly which Methods took so long and on top of the table is (which with distance longer than any other methods):

Quote:
java.lang.ClassLoader.loadClass(String)

EDIT : This may not be relevant, but i will post it anyway: This took also very long.
Quote:
org.hibernate.internal.SessionImpl.initializeCollection(PersistentCollection, boolean)

Does anyone have some idea why ?

The Methode i used to fetch objects :

Quote:
List<FitArticle> list = new ArrayList<FitArticle>();
Session session = factory.openSession();
Transaction tx = null;
Criteria crit = session.createCriteria(FitArticle.class);
// Here some lines to modify Criteria.
list= crit.list();
return list;



Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Working with fetched objects take too long.
PostPosted: Tue Apr 04, 2017 11:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
As I explained in this article, there is no reason why you'd want to fetch 300 entities into the UI?

You can't display that many entries at once so try the following optimizations:

1. Use pagination
2. Use DTO projections instead of fetching entities

The performance improvement is going to be significant.


Top
 Profile  
 
 Post subject: Re: Working with fetched objects take too long.
PostPosted: Tue Apr 04, 2017 11:56 am 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
vlad wrote:
As I explained in this article, there is no reason why you'd want to fetch 300 entities into the UI?

You can't display that many entries at once so try the following optimizations:

1. Use pagination
2. Use DTO projections instead of fetching entities

The performance improvement is going to be significant.



Hi, thanks for your answer.

I think its not about fetching how much, because i tested with only 40, its still as slow.

Furthermore. The Problem is actually rendering the List of objects (fetched by Hibernate) is way slower than the list of the same objects ( fetched with JDBC method):

I used the Profiler and determined that the initializeCollection() of hibernate is invoked everytime i want to fetch a list of objects (which might be why its slow), how can i prevent it ?


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 11:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Fetching 40 entities should not take 200ms. Send us a replicating test case that proves your assumptions.


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 12:01 pm 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
Yeah in case 40 entity its like 10-20 ms

I will try to do it asap


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Are you using the right database indexes? IS it just a pet project with 100 entries that does a full table scan anyway?


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 12:24 pm 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
vlad wrote:
Are you using the right database indexes? IS it just a pet project with 100 entries that does a full table scan anyway?


Yeah i am using the actuall database at work, so it must be right, the thing that bugs me is only so, that it takes longer to render the result list than before (while i was using JDBC to get the list), its like 1 sec and 4 secs, which is not an acceptable performance for my programm. I figured out that the methode initializeCollection is called EVERYTIME i try to fetch a list, could it be a reason ?


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Without a proper profile to demonstrate where the time is being spent, you might follow false leads. As i told you, we need to see a real example that proves your assumptions. Try building a replicating test case.


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 2:49 pm 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
While im trying to figure out a test case (a bit complicated for me to fathom), i regconize that when i print out some of the properties of my objects, the one with subclass-superclass higharchie took the longest to load ! Although they are all short strings, but the printing time of 40objects are so different, like between printing ID , and some string from a subclass, its between 1ms and 4000ms.

Could that be a problem ? , i dont use LazyFetching.


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 3:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There you go. As already stated, you are fetching more than necessary. Switch to DTOs and everything will work fine. ;)


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Tue Apr 04, 2017 6:08 pm 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
i wish it were, but i still need those properties, the only thing i recognized that loading to these property took way way longer than those primitive properties. Is there any way that the mapping style would affect the loading time ?


Top
 Profile  
 
 Post subject: Re: Fetching Hibernate entities take too long
PostPosted: Wed Apr 05, 2017 12:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Yes, it is. Mapping choices have a significant impact on performance, check out these two articles:

- The best way to map a @OneToMany relationship with JPA and Hibernate
- The best way to map a @OneToOne relationship with JPA and Hibernate

and especially this one about EAGER fetching.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.