-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mass Data export
PostPosted: Wed Mar 11, 2009 5:50 pm 
Newbie

Joined: Wed Mar 11, 2009 5:13 pm
Posts: 4
Hi, i have to export a huge amount of datasets. My whole app works with hibernate and spring.
I have to export 300.000 to 3.000.000 Datasets...

At the Moment i do it that way
Code:
public List<ResellerArticle> getResellerArticlesForExport(final int resellerId,final boolean justUpdate) {
      return (List<ResellerArticle>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
                Query query = session.createQuery("select a from ResellerArticle a left outer join fetch a.baseArticle  where a.resellerId = ? "+(nurUpdate?"and justUpdate = 1)":""+ "   " ));
                query.setInteger(0, resellerId);               
                return query.list();
            }
        });      
   }

here i get some problems with the memory... i cant load all data and than write it...

should i work with an iterator or
query.setFirstResult(..);
query.setMaxResults(...);

or whats the best way in sucha case?

thanks for you help...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 8:20 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
We are using a StatlessSession and Query.scroll() with successful result. We are currently exporting well over 10 million rows with almost no memory overhead and as fast as the db can deliver the result.

* A StatelessSession doesn't have a first-level cache as the Session does. It may work with a Session if you call Session.clear() at regular intervals. If you use a StatelessSession make sure that many-to-one references are either proxied of fetch joined or the result is (many) additional queries.

* Query.scroll() is needed because Query.iterate() only selects the id:d in the first query and then uses an additional select to load each item. This can take a very long time.

* Query.setFirstResult() and Query.setMaxResult() together with Query.list() also works, but since you need to repeat the query multiple times with an increasing value for the Query.setFirstResult() it becomes slower and slower the more rows you need to export.

* Make sure that your database setup support server-side cursors. We ran into this problem with MySQL were the JDBC driver tried to load the entire result into memory. Before this was solved (by MySQL a couple of years ago) we were forced to use the Query.setFirstResult()/Query.setMaxResult() approach.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 4:48 am 
Newbie

Joined: Wed Mar 11, 2009 5:13 pm
Posts: 4
very helpfull, thank you.

i tried Query.iterate() at the beginning, it was realy slow, .. hibernate produced a select for every row...

i'm using Query.setFirstResult()/Query.setMaxResult() at the moment, i have no performance problems, 300k rows in 30 sec, at the moment thats ok

i'll try the Query.scroll() next week


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.