Hibernate bulk Query: session.find/iterate

Source: Internet
Author: User
Tags memory usage
Query performance is often an important aspect of system performance, and the query mechanism determines the overall performance of the system to a large extent. This area often also has the biggest performance adjustment space.

The Session.find () in Hibernate2 corresponds to the Session.createquery (). List () in 3.
The Session.iterate () in Hibernate2 corresponds to the Session.createquery () in 3 (). Iterate ();
Find and Iterate difference:
The Find method implements the query operation through a select SQL, and the iterate method executes multiple select SQL.
Iterate the first query gets the IDs of all eligible records, and then reads the corresponding records from the library table based on each ID, a typical n+1 query problem that requires 10,001 select SQL if a qualifying record is 10,000. Think about how bad performance will be.

So why provide the iterator method, not just provide efficient find method.

Reason 1. Closely related to the Hibernate caching mechanism
The Find method is actually unable to take advantage of the cache, which writes only unread to the cache.
The Find method executes only one SQL query, it cannot determine what data in the cache is eligible, nor does it guarantee the integrity of the query results. The iterate method will first query all the IDs that match the condition records, and then go to the cache according to the ID, if the cache has the ID, return, no database query can be based on ID.
String hql = "from TUser where Age >?";
List userlist = Session.find (hql, New Integer (), Hibernate.integer);
Iterator it = session.iterate (hql, New Integer (), Hibernate.integer);
Sequential execution, the iterate method executes only one SQL query, the lookup ID, and then the data can be obtained from the cache based on the ID.

String hql = "from TUser where Age >?";
List userlist = Session.find (hql, New Integer (), Hibernate.integer);
UserList = Session.find (hql, New Integer (), Hibernate.integer);
caching is not working.
If the target data is read relatively frequently, it can reduce the performance loss by iterate this mechanism.

Reason 2. Memory Usage Considerations
The Find method will get all of the records at once and read them into memory. If the amount of data is too large, outofmemoryerror may be triggered, resulting in system exceptions. One solution is to combine the iterate method and the evict method to process records, keeping the memory within an acceptable range. Such as:
String hql = "from TUser where Age >?";
Iterator it = session.iterate (hql, New Integer (), Hibernate.integer);
while (It.hasnext ()) {
TUser user = (TUser) it.next ();

Remove an object from a first-level cache
Session.evict (user);

Second-level caching can set the maximum amount of memory, to automatically abolish older data, but can also be edited
Code removal, which helps keep data valid.
Sessionfactory.evict (Tuser.class, User.getid ());

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.