1

I am having around 1000 different id's for which I need to run the below hibernate query as many times. Based on the records retrieved (the record need not be present) , I do a bit of processing before entering it in another table.

The issue here is the whole process of retrieving the records takes approx 20-25 sec. Is this normal? Can the query be tweeked further to reduce the time?

(The below hibernate query will be executed for each id)

String query = "select emp from Employee emp where emp.id = '"+id"' and emp.status.statusCd = 'A'";

Note: Employee is a table with around 30 columns. status_cd and id are VARCHAR.

2
  • don't concatenate parameters into your queries, it's a sql injection waiting to happen >_< Mar 7, 2012 at 13:56
  • @NathanHughes I have taken care of that ;-)
    – Chillax
    Mar 7, 2012 at 14:00

1 Answer 1

4

First of all, never use string concatenation to set variable fields in SQL.

String queryString = "select emp from Employee emp where emp.id = :id and emp.status.statusCd = 'A'";
Query query = Session.createQuery(queryString);
query.setInteger("id", 168);

Second, you could try to get all the target records with one query, docs:

String queryString = "select emp from Employee emp where emp.id in (:id) and emp.status.statusCd = 'A'";
Query query = Session.createQuery(queryString);
Collection<Integer> idList = new ArrayList<Integer>();
query.setParameterList("id", idList);

Then you would iterate over the results of this query and process each record as you processed them previously.

Third, if you execute additional SQL while processing each individual record, then you should also collect them and execute them in a batch.

1
  • +1 for the introduction of the setParameterList method! I used to make a coma separated parameter list myself!
    – ManuPK
    Mar 7, 2012 at 14:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.