Optimization of Hibernate (Optimization guidelines---cartesian product problem)

Source: Internet
Author: User

Optimization of Hibernate (Optimization guidelines---cartesian product problem)

Cartesian product problem

In contrast to the n+1 query problem, a SELECT statement that captures too much data. If you try to crawl a few "parallel" sets, this Cartesian product problem will not occur. Suppose you have decided to apply a global fetch= "join" setting to a bids collection of item. The item class has other collections (for example, images). Also assume that you decide that all pictures of each item must be loaded immediately through the fetch= "join" policy:

<class name= "Item" > ... <set name= "bids" inverse= "true" fetch= "join" > <key column= "item_id"/> < One-to-many class= "Bid"/> </set> <set name= "Images" fetch= "join" > <key column= "item_id"/> < Composite-element class= "Image" > ... </set> </class>

If you map two parallel collections (same as their own entities) through an immediate external join crawl strategy, and load all the item objects, hibernate executes a SQL SELECT that creates the product of the two collections:

Select item.*,bid.*. Image.* from item left OUTER join BID BID on item. item_id = bid. item_id left OUTER join Item_image IMAGE on ITEM. item_id = image. item_id

This result set contains a large amount of redundant data.

If you map this parallel collection with fetch= "Subselect", you get 3 queries: This is the recommended optimization for a parallel set. However, there are exceptions to every rule. As long as the collection is small, the product is likely to be an acceptable crawl strategy. Note that parallel single value associations that are immediately crawled through an external join select Do not generally produce a product (that is, when using fetch= "join", in the case of <many-to-one>,<one-to-one> these single value associations, does not produce Cartesian product, if used in the set, there will be Cartesian product phenomenon. )

Finally, although Hibernate lets you create a Cartesian product in 2 (or even more) parallel collections by fetch= "Join", it throws an exception if you attempt to enable fetch= "join" on a parallel <bag> set. The result set of the product cannot be converted into a package collection because Hibernate cannot know which rows contain valid duplicates and which rows do not contain them. If you use a package collection, you do not enable the crawl policy that causes the product. Parallel instant crawl to the package set is crawled using a subquery or an immediate two-level query.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.