"Java" Itoo Project combat hibernate lazy Load Optimization performance

Source: Internet
Author: User

in doingItoo 3.0, the evaluation system wanted to go online, began to import data, only to import students2data, but the speed of the import is particularly slow, the reason for this slow is because of the excessiveIOoperation. But after the import was successful, the students were queried more slowly because the underlyingHibernateof thehqlThe statement is queried, learnedHibernatethe people know that ifHibernatedo not set lazy load, only the relevant data will be all at once query out, I tried, query2million data, the deepest cascade query is there5layer, and then send out the statement is460, the time is probably10s. Then, consider usingLazybe optimized. And then you start toLazyhas carried out a study.

Q: Can lazy loading improve performance ?  

can not simply say "can", because Hibernate's relational mapping dragged down the performance of SQL, so think of lazy loading to compensate. Just make up and not go beyond. So let's not think about the use of lazy loading overall performance is improved, in fact, the overall performance does not fall on the lucky.

Q: Where can hibernate's lazy properties be configured?

(Common) <set><list> label , can be value True,false,extra The default is true, when true, lazy loading, and then issuing SQL statements when accessing the collection properties. But Set.size () is not smart and does not generate a count () statement, but instead finds all records assigned to set.

Extra Description: When calling the collection's Set.size (), the SelectCount (*) from TableName is generated, which is more intelligent. Recommended Use

(less) <many-to-one><one-to-one> on label , you can take the value False,proxy,noproxy when True, there will be lazy loading characteristics, when false will produce n+1 problems, such as a student corresponding to a class, Using a SQL to isolate 10 students, hibernate will generate 10 SQL to identify each student's class when accessing the student's class attributes.

(not) <class> on label , you can take the value True,false default to True, when false, the load () method loses the lazy load property as with Get (), without affecting the collection (<set ><list>) The lazy feature on the label

(NO) <property> label , can be value true,false The default value is False, lazy loading a field, meaningless, do not use

Q: What is the difference between get () and load ()

Get () No lazy load feature, execute SQL query immediately.

Load () has lazy loading characteristics, will be returned to a proxy object, so never null, first do not execute SQL, to fetch the value of the object to execute the SQL statement, the previous session can not close the,<class> label lazy is not false.

Q: Using lazy loading conditions

1 PO cannot be final.

2 objects that can be lazy loaded (PO) are proxy objects that are overwritten by cglib, so they cannot be final decorated.

3 need to asm,cglib two jar packages

4 The corresponding lazy property is True

5 The corresponding fetch attribute is select  

Q: When do I encounter lazy loading

1 Using Load ()

21 pairs a <one-to-one>  

The Master object uses join connection by default and lazy loading does not occur

Check from object default will occur lazy loading, first execute a select isolated from the object, when the main object by accessing from the object, then execute a select to isolate the main object.

More than 3 to one <many-to-one>

When using HBM.XNL, when taking more than one side, the default will be lazy loading, do not take a party

When using JPA, when taking more than one party, the default automatically uses the Joinon statement to take out a party (user and group, the user is more than one party, the group is a party)

41 to More (<set><list>)

The default is lazy loading, which is required and is heavily used.

Q: What are the scenarios for lazy loading?

method One: (Lazy loading is not used)    

Use Hibernate.initialize (De.getemps ()) to load it in advance.  

Method Two:

Rebind the object that was detached from the session

The lock () method is used to allow the application to re-associate an unmodified object to the new session method.

// Direct Re-association

Ssion.lock (Fritz,lockmode.none);

// associating after a version check

Session.lock (Izi,lockmode.read);

// Use SELECT ... Forupdate after version Check Association

Session.lock (Pk,lockmode.upgrade);

Method Three:

Opensessioninview

Opensessioninviewfilter is a support class for hibernate provided by spring, which mainly means to open the session of Hibernate when initiating a page request, and keep this session, Until this request is completed, specifically through a filter.

Because Hibernate introduces the Lazyload feature, Hibernate throws a Lazyload exception if the object that is out of Hibernate's session is again trying to fetch the value of its associated object through the Getter method. So in order to solve this problem, spring introduces this filter, which makes the life cycle of hibernate session grow longer.

Specific reference: http://www.iteye.com/topic/32001

Q: How fetch and lazy configurations are used for data queries

The lazy parameter values are often false and the default lazy = True in the true,hibernate3 mapping file;  

fetch Specifies the way in which the associated object is fetched, the parameter values are usually select and join, the default is to select,select the main object, and then according to the associated foreign key, each object sends a select query, obtains the associated object, forms the N+1 query The Join method is the left Outerjoin query, and the main object and the associated object are queried at the same time with the SQL associated with a foreign key, and no multiple queries are formed.  

in the mapping file, different combinations use different queries:  

1, lazy= "true" fetch = "select", using the delay policy, start querying only the main object, the association object will not query, only when the use of the time will be issued SQL statements to query;  

2, lazy= "false" fetch = "select", do not use a delay policy, while querying out the main object and associated objects, resulting in 1+n SQL.  

3, lazy= "true" or lazy= "false" fetch = "join", the delay will not work, because the use of an outer join query, while the main object and associated objects are queried.  

In addition, in the HQL query, the join method set in the configuration file is not working, and in other query methods such as GET, criteria, etc. are valid, use Select Mode, unless a join is specified in HQL to fetch an associated object. Fetch policy is used to get/load an object, how to get a non-lazy object/collection. These parameters are not valid in query.

Finally, because the front-end framework uses Easyui , when data is bound, the data in the background needs to be converted into JSON string, using lazy loading, cascade query data will be a problem when the conversion, so UI after The lazy load of the way to abandon, the next section does not introduce another way to optimize.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java" Itoo Project combat hibernate lazy Load Optimization performance

Related Article

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.