-->
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: Query Building And Performance
PostPosted: Tue Jan 10, 2006 9:13 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
Does anyone know if the way you build your query has any effect on its performance?

Example:

String aCatsName = "Tom";
int aCatsAge = 2;
List list = null;

Query query = HibernateHelper.getSession().createQuery("select Cat from Cat where Cat.name = :catname and Cat.age > :catage");
query.setString("catname", aCatsName);
query.setInteger("catage", aCatsAge);
list = query.list();

--OR--

String aCatsName = "Tom";
int aCatsAge = 2;
List list = null;

Query query = HibernateHelper.getSession().createQuery("select Cat from Cat where Cat.name = " + aCatsName + " and Cat.age > " + aCatsAge);
list = query.list();

When you call createQuery does Hibernate initialize anything? When you start "setting" parameters, does Hibernate begin "filtering" the data to work with a smaller subset?

Obviously the first example is "cleaner", I was just curious about how it works under the hood to understand which is more efficient, or if it gets executed the same way.

Thanks for your help.

Brian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 12:35 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
As far as hibernate is concerned, there is no difference.

As far as java is concerned, the first option is faster because the string + string approach uses StringBuffer, which is synchronized. Also, java can't put the entire string in its string table.

As far as JDBC is concerned, the first approach is faster, because it creates a PreparedStatement instead of a simple Statement. PreparedStatements are precompiled and stored... somewhere. In driver memory, or in the server, I don't know. Have a look at the JDBC javadocs for more info.

As far as "clean" programming goes, I would recommend putting all queries into .hbm.xml files as named queries. That way. if you ever have to modify a query in use by in-production software, you only have to re-distribute mapping files instead of recompiling entire projects.


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.