Wednesday, January 30, 2008

Can Hibernate Learn from Edge Rails?

Just finished reading about some eager-loading options for edge rails, and how they've moved away from the outer-join approach and into a multi-query approach because they believe it to be faster.

Last I saw, Hibernate used the outer-join approach in its internals for eager-fetches, so I wonder, is this an instance where Hibernate can learn from edge rails? I'd be curious to evaluate the two approaches across all the databases Hibernate supports with different data sets and see which wins out.


sqrrrl said...

Hibernate already has similar capabilities. Check out the subselect and batch fetch strategy in the docs.

About two years ago I had a co-worker who thought hibernate couldn't generate efficient queries for certain cases. He had some hand-written code that worked on a complex object graph that was about 5 levels deep with several collections and he spent probably a week trying to optimize the code. It took me about 3 hours rewrite the data access code to use hibernate and with the right fetching strategies set the hibernate generated functionally equivalent SQL to what was hand written and was able to load the object graph in the same number of round trips :)

Gavin said...

Correct, Hibernate has had this option for several years.

Subselect fetching is more efficient in cases where you need to fetch more than one to-many association (ie. collection) in *parallel*, and avoids the cartesian product that would be required for outer join fetching in that case.

However, note that fetching of multiple parallel to-many associations is actually extremely rare in practice. It is far more common that the to-many's would occur in *series*, in which case outer join fetching is likely to be more efficient.

Geoffrey Wiseman said...

Ah, well, no learning, then. Can Edge Rails learn from Hibernate? :)

Thanks for the tips, Gavin/Sqrrl. I'll have to check it out.

Geoffrey Wiseman said...

Ah - the subselect portion of the documentation could use a bit of a boost, but the overall description of fetching strategies is good.