1

We have a Users table (MySQL) with 120,000 rows

List<User> users = session.createQuery("from User").list();

This hibernate query takes about 6 to 9 seconds to execute. How can we optimize this? Is MySQL the bottleneck? Or is .list() usually this slow?

2
  • Where you will display result? Commented Oct 5, 2013 at 17:30
  • @Masud, the results will not be displayed. we need the result set for some processing. Commented Oct 5, 2013 at 17:38

2 Answers 2

1

Ofcourse it's slow because the query perform the full table scan. You should join other objects associated with it, including where clause of the query, the query could be changed to return the limited number of records or use criteria API and projection.

Sign up to request clarification or add additional context in comments.

4 Comments

unfortunately we need to the entire table. so we can't use the criteria API or projection or WHERE clause
then you have no choice
Is mySQL the bottleneck? Or the CPU which is running tomcat? or the memory?
then what is causing it to be so slow?
1

Use pagination on your query. You should not call all rows at a time. You can set first position of result and maximum result. For example, if you want to read first 100 result than change your query like:

Query q=session.createQuery("from User");
q.setFirstResult(fistRes);// this will variable
q.setMaxResults(maxRes);// this will also be variable as parameter.

1 Comment

@SaqibAli This will partition your result into different page.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.