Hibernate version: 2.1.8
Name and version of the database you are using: Posgresql 7.3.2
Description of problem
In postgresql, using an explicit join order can cause significant performance problems (in my case, 25 seconds instead of 0.2 seconds if the wrong join order is used).
See
http://www.postgresql.org/docs/7.4/stat ... joins.html
An explicit join order is unfortunately enforced whenever you use standard inner join syntax:
(1) select a.id, b.id from a inner join b on a.id=b.aid
If you rewrite the query as
(2) select a.id, b.id from a, b where a.id=b.aid
postgresql allows its query planner to do its thing, which is generally the wisest thing to do.
So, what I've been doing is rewriting all of my HQL to look like (2) above. This works for performance. The problem comes with eager fetching.
Since the join is an integral part of the fetch syntax, I see no way to rewrite the HQL to give the planner a free hand at planning.
What I get for the following HQL:
HQL:
SELECT definition.id FROM DefinitionImpl AS definition inner join fetch definition.termesDB AS terme where definition.fiche.id=?
The generated SQL:
select termesdb1_.idTerme as idTerme, termesdb1_.version as version, termesdb1_.idDefinition as idDefini3_, termesdb1_.idEnReponseA as idEnRepo4_, termesdb1_.texte as texte, termesdb1_.textePourTri as textePou6_, termesdb1_.statut as statut, termesdb1_.type as type, termesdb1_.registre as registre, termesdb1_.genre as genre, termesdb1_.idProposePar as idPropo11_, termesdb1_.idRevisePar as idRevis12_, termesdb1_.dateCreation as dateCre13_, termesdb1_.dateMiseAJour as dateMis14_, termesdb1_.idDefinition as idDefini3___, termesdb1_.idTerme as idTerme__, definition0_.idDefinition as x0_0_
from Definition definition0_ inner join Terme termesdb1_ on definition0_.idDefinition=termesdb1_.idDefinition where (definition0_.idFiche=? )
What I would like to get:
select termesdb1_.idTerme as idTerme, termesdb1_.version as version, termesdb1_.idDefinition as idDefini3_, termesdb1_.idEnReponseA as idEnRepo4_, termesdb1_.texte as texte, termesdb1_.textePourTri as textePou6_, termesdb1_.statut as statut, termesdb1_.type as type, termesdb1_.registre as registre, termesdb1_.genre as genre, termesdb1_.idProposePar as idPropo11_, termesdb1_.idRevisePar as idRevis12_, termesdb1_.dateCreation as dateCre13_, termesdb1_.dateMiseAJour as dateMis14_, termesdb1_.idDefinition as idDefini3___, termesdb1_.idTerme as idTerme__, definition0_.idDefinition as x0_0_
from Definition definition0_, Terme termesdb1_ where definition0_.idDefinition=termesdb1_.idDefinition and (definition0_.idFiche=? )
Is there any way to get Hibernate to produce this SQL while benefiting from eager fetching?
Ideally, there could be some Hibernate configuration setting added for the postgresql dialect, which automatically changes all inner joins for all queries to a free syntax, going from a inner join b on a.id=b.aid to a, b where a.id=b.aid
Best regards,
Assaf