Hibernate batch operation hibernate. JDBC. batch_size hibernate. JDBC. fetch_size

Source: Internet
Author: User
Several suggestions for optimizing hibernate Performance

1. for Oracle databases, fetch size is the number of records retrieved from the database each time the JDBC Statement reads data. It is generally set to 30, 50, and 100. The default fetch size of the JDBC driver for Oracle database is 15, and the fetch size is set to 30 and 50. The performance will be significantly improved. If it continues to increase, it will exceed 100, and the performance improvement will not be obvious, instead, the memory will be consumed.

In the hibernate preparation file:

1 <property name = "hibernateproperties">
2 <props>
3 <prop key = "hibernate. dialect"> org. hibernate. dialect. oracle9dialect </prop>
4 <prop key = "hibernate. show_ SQL"> false </prop>
5 <! -- Create/update the database tables automatically when the JVM starts up
6 <prop key = "hibernate. hbm2ddl. Auto"> Update </prop> -->
7 <! -- Turn batching off for better error messages under PostgreSQL
8. <prop key = "hibernate. JDBC. fetch_size"> 100 </prop> -->
9 <prop key = "hibernate. JDBC. batch_size"> 50 </prop>
10 </props>
11 </property> the greater the fetch size, the less the number of reads to the database, and the faster the speed. The smaller the fetch size, the more reads the database, and the slower the speed.
Hibernate. JDBC. fetch_size: a non-zero value that specifies the size of the number of JDBC crawlers. Statement.setFetchSize()).
  1. /**
  2. * Gives the JDBC driver a hint as to the number of rows that shoshould
  3. * Be fetched from the database when more rows are needed. The number
  4. * Of rows specified affects only result sets created using this
  5. * Statement. If the value specified is zero, then the hint is ignored.
  6. * The default value is zero.
  7. *
  8. * @ Param rows the number of rows to fetch
  9. * @ Exception sqlexception if a database access error occurs, or
  10. * Condition 0 <= <code> rows </code> <= <code> This. getmaxrows () </code>
  11. * Is not satisfied.
  12. * @ SINCE 1.2
  13. * @ See # getfetchsize
  14. */

Hibernate. JDBC. batch_size:

Non-zero value, allowing hibernate to use batch update of jdbc2.ValueRecommended5To30. Set the maximum number of SQL statements that can be submitted at a time to improve the efficiency of SQL statement execution.

You can configure
<Session-factory>
...
<Property name = "hibernate. JDBC. batch_size"> 50 </property>
...
</Session. Factory>
When we initiate an SQL call, 50 SQL statements are accumulated and then submitted in batches.
One session created by the same sessionfactory performs batch operations, executes 100 SQL statements, and performs two operations on the database.

 

2. If it is an ultra-large system, it is recommended to generate an HTM file. Speed up page improvement.

3. Do not place all responsibilities on Hibernate, refactor the code, reduce database operations, avoid using in operations in database queries, and avoid recursive query operations, the rationality of code quality and system design determines the system performance.

4. When querying large data volumes, use List () or iterator () to return the query results with caution,

(1) When list () is used to return results, Hibernate initializes all query results as persistent objects. When the result set is large, it takes a lot of processing time.

(2 ). when iterator () is used to return results. when next () returns an object and uses the object, Hibernate calls the query to initialize the corresponding object. For a large amount of data, it takes a lot of time to call each query. Iterator () is advantageous when the result set is large but contains a large amount of the same data, or not all of the result sets are used.

5. Using the delayed loading mechanism in the one-to-many and many-to-one relationship will enable initialization of many objects during use, which can save memory space and reduce database load, in addition, if the set in the Po is not used, the interaction between databases can be reduced to reduce the processing time.

6. If default-cascade = "all" or "Save-Update" is used for a PO (persistence object) with an association, pay attention to the value assignment operation for the set in the Po, because it is possible to execute an update operation more than once.

7. The number of interactions with databases is the most important factor determining the processing time for new, modify, or delete operations on large data volumes or queries on large data volumes, reducing interactions is the best way to improve efficiency. Therefore, during the development process, set show_ SQL to true to gain an in-depth understanding of the hibernate processing process and try different methods, this improves efficiency. Minimize the number of database operations on each page. The fewer the better.
-------------------------------------------------------------

Batch insert

During project development, due to project requirements, we often need to insert large amounts of data into the database. Tens of thousands, 100,000, millions, or even tens of millions. If you use hibernate to insert data of such magnitude, an exception may occur. The common exception is outofmemoryerror (memory overflow exception ).
Basic Idea: optimize Hibernate and set hibernate in the configuration file. JDBC. the batch_size parameter is used to specify the number of SQL statements submitted each time. The program uses the multipart insert method to promptly clear the cache (Session implements asynchronous write-behind, it allows hibernate to explicitly write the batch operation), that is, after inserting a certain amount of data, they are promptly removed from the internal cache to release the occupied memory.
Set the hibernate. JDBC. batch_size parameter. The configuration is as follows.
<Hibernate-configuration>
<Session-factory>
.........
<Property name = "hibernate. JDBC. batch_size"> 50 </property>
.........
<Session-factory>
<Hibernate-configuration>

The reason for configuring the hibernate. JDBC. batch_size parameter is to read the database as little as possible. The larger the value of the hibernate. JDBC. batch_size parameter, the fewer reads the database and the faster the speed. From the configuration above, we can see that hibernate waits until the program has accumulated 50 SQL statements and then submits them in batches.

The value of the hibernate. JDBC. batch_size parameter may not be set to a larger value, the better. It is still to be discussed in terms of performance. This should take into account the actual situation, set as appropriate, generally set 30, 50 can meet the needs.
Insert 10000 data records as an example:

Session session = hibernateutil. currentsession ();
Transatcion Tx = session. begintransaction ();
For (INT I = 0; I <100000; I ++)
...{
Student s = new student ();
S. setname ("PAUL ");
Session. Save (s );
If (I % 50 = 0) // each 50 pieces of data is used as a processing unit
...{
Session. Flush (); // synchronize with database data
Session. Clear (); // clear all data in the internal cache and release the occupied memory in time
}
}
TX. Commit ();
.........

Under a certain data scale, this method can maintain the system memory resources in a relatively stable range.

If the second-level cache is enabled, Hibernate will add data to the second-level cache during insert, update, and delete operations to maintain the second-level cache. The performance will suffer a lot. We recommend that you disable the second-level cache in the case of batch processing.

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.