I'm using Liferay 4.2.1, and found some weird result with hibernate query.
There's a query with an outer join, and here is the portion of the hql:
Code:
StringBuffer hql = new StringBuffer();
hql.append("from com.liferay.portlet.journal.model.impl.JournalArticleImpl as article ");
hql.append("left join fetch article.categories cat ");
hql.append("where cat.id=? ");
hql.append("and article.primaryKey.groupId = ? ");
hql.append("and article.approved=true ");
hql.append("and article.expired=false ");
hql.append("and article.displayDate <= ? ");
hql.append("and (article.expirationDate is null or article.expirationDate > ?) ");
hql.append("and article.primaryKey.version =(select max(a.primaryKey.version) from com.liferay.portlet.journal.model.impl.JournalArticleImpl as a where a.primaryKey.articleId = article.primaryKey.articleId) ");
hql.append("order by article.displayDate desc");
This part of the code does not seem to be much of problem, I'm running it on my machine (slow machine), with 1GB of memory, there are 2000 rows of data, and getting all of them requires about 1 second.
Now, when I added the following for pagination:
Code:
query.setFirstResult(beginIndex);
query.setMaxResults(max);
and then
Code:
Date t1 = new Date();
query.list();
Date t2 = new Date();
And setting max to 10, it takes 3 to 5 times longer than if I just retrieve the whole list (without setFirstResult() and setMaxResults()). It takes 3 to 5 seconds to get 10 records. This does not make any sense.
As the amount of data grows, it is getting slower and slower. If I paginate over 100 records, the performance is ok, but still slower than getting the whole 100 records. But as data grow, it is getting slower.
Using setFirstReuslt() and setMaxResults() is the default method for paginating, but this is killing us.
It takes 3 to 5 time longer to get 10 records than retrieving the whole 2000 records!!! Now I can get the whole list, then spending a few more millisecs using sublist() to get the same 10 records, and I'm still better off, time-wise. But this is not the way to go, resources-wise.
I'm using:
OS: Linux (Ubuntu 07.04)
Liferay 4.2.1
PostgreSQL 8.1.10
Java 1.5.0_08
Could someone give a hint what is going on here?
thanks
weird coder