-->
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.  [ 2 posts ] 
Author Message
 Post subject: Performance question on loading large number of rows
PostPosted: Fri May 25, 2012 12:06 pm 
Newbie

Joined: Fri May 25, 2012 11:43 am
Posts: 1
Hibernate/JPA newbie here, and I have a question on performance when loading a large number of rows - about 200,000 in my test case. I'm comparing performance vs. straight JDBC and have turned off JPA caching. My test with Hibernate 6.1 took about 6.1 seconds to load 160k rows from a single table with no joins, while the JDBC version took about 3.2 seconds. Looking for opinions and comments as to why it is so comparatively slow. Here is the relevant code:

JPA version:
public List<FieldSecurity> loadFieldSecurity() {
List<FieldSecurity> list = null;
EntityManager em = getEntityManagerFactory().createEntityManager();
StringBuilder sql = new StringBuilder();
sql.append("select FieldSecurity.* from FieldSecurity ");
sql.append("where FieldSecurity.userId is null OR FieldSecurity.userId=0");
list = (List<FieldSecurity>)em.createNativeQuery(sql.toString(), FieldSecurity.class)
.getResultList();
em.close();
return list;
}

JDBC version:
public List<FieldSecurity> loadFieldSecurity() {
List<FieldSecurity> list = null;
StringBuilder sql = new StringBuilder();
sql.append("select FieldSecurity.* from FieldSecurity ");
sql.append("where FieldSecurity.userId is null OR FieldSecurity.userId=0");
try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:as400://system/library","user","password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql.toString());
list = new LinkedList<FieldSecurity>();
while (rs.next()) {
FieldSecurity fs = new FieldSecurity();
FieldSecurityKey key = new FieldSecurityKey();
key.setApplicationId(rs.getLong("applicationId"));
key.setRoleId(rs.getLong("roleId"));
key.setFieldId(rs.getLong("fieldId"));
key.setUserId(rs.getLong("userId"));
fs.setKey(key);
fs.setAccessType(rs.getString("accessType").charAt(0));
list.add(fs);
}
rs.close();
stmt.close();
conn.close();
}
catch (Throwable t) {
t.printStackTrace();
}
return list;
}


Top
 Profile  
 
 Post subject: Re: Performance question on loading large number of rows
PostPosted: Fri Jun 01, 2012 7:27 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
One possible explanation could be that Hibernate is not just loading your data from the database, it is also putting it into a managed state where it will track your entities and automatically handle any updates.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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