Hibernate speed problems--hibernate.jdbc.fetch_size and hibernate.jdbc.batch_size

Source: Internet
Author: User
Tags bulk insert reflection

Hibernate's speed problem


I also wondered, I also felt very slow at the initial application of hibernate, knowing that I knew the problem later.
In fact, hibernate speed performance is not bad, compared to JDBC, but also performance can be twice times higher.
Of course, this is related to the application database, Hibernate supports Hibernate.jdbc.fetch_size and hibernate.jdbc.batch_size on Oracle, and MySQL does not support it. Most of my original projects are using MySQL, so I feel slow, in fact, in enterprise applications, especially in the financial system large-scale applications, using Oracle more, relatively speaking, hibernate will improve the system a lot of performance.

Hibernate.jdbc.fetch_size 50//Read

Hibernate.jdbc.batch_size 30//write

Hiberante.cfg.xml (Oracle, SQL Server support, MySQL not supported)

<property name= "Hibernate.jdbc.fetch_size" >50</property>
<property name= "Hibernate.jdbc.batch_size" >30</property>

These two options are very, very important!!! Will severely affect the crud performance of Hibernate!

C = Create, R = read, U = update, D = delete

Fetch Size is the number of record strips that are fetched from the database each time the JDBC statement reads data.

For example, to query 10,000 records at a time, for the JDBC driver of Oracle, it is not 1 times to take out 10,000, but only fetch the number of fetched size, when the record set traversed these records, then go to the database fetch the size bar data.

Therefore, the unnecessary memory consumption is greatly saved. Of course, the larger the fetch size is, the fewer times the database is read, and the faster the fetch size is, the more times the database is read, the slower the speed.

It's kind of like when we write a program to write a hard disk file, set up a buffer, each write buffer, and so on after the buffer is full, once written to the hard disk, the same reason.

The JDBC driver default Fetch size=10 for Oracle database is a very conservative setting, according to my test, when fetch SIZE=50, performance will increase 1 time times more, when fetch size=100, performance can continue to improve 20%,fetch Size continues to grow, and the performance gains are not significant.

So I recommend that you use Oracle to set the fetch size to .

However, not all databases support the Fetch size feature, for example MySQL is not supported.

MySQL is like the worst case I've ever said, and he always takes 10,000 records completely out of the way, and memory consumption can be very, very amazing! There's no good way out of this situation:(

Batch size is the batch size that is set for bulk delete, batch update, and BULK INSERT, which is a bit equivalent to setting buffer buffer size.

The larger the batch size, the smaller the number of times the batch operation sends SQL to the database, the faster it gets. One of the tests I did was that when batch size=0, it would take 25 seconds to delete 10,000 records from the Oracle database using Hibernate, and Batch Size = 50, removing just 5 seconds!!!

//
We typically do not manipulate an object's identifier (identifier) directly, so the setter method of the identifier should be declared private (private). This way, when an object is saved, only hibernate can assign an identifier to it. You will find that Hibernate has direct access to methods (accessor method) and fields (field) that are declared as public,private and protected for different levels of access control. So choosing which way to access the property is entirely up to you, and you can make your selection match your program design.

All persistent classes (persistent classes) require a parameterless constructor (No-argument constructor), because hibernate must use the Java Reflection Mechanism (Reflection) to instantiate the object. The access control of the constructor (constructor) can be private, but when the runtime proxy is generated it will require access control at least package level, so that no bytecode is programmed (bytecode instrumentation), it is more efficient to get data from a persisted class. and

Hibernate.max_fetch_depth Set the maximum depth of the outer join crawl tree

Values are taken. The recommended setting is between 0 and 3

Is that every time you query, the depth of the Cascade query, such as you set the association VO Eager, if the fetch_depth value is too small, will send many more SQL.

Transferred from: http://blog.csdn.net/a9529lty/article/details/6660118

Hibernate speed problems--hibernate.jdbc.fetch_size and hibernate.jdbc.batch_size

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.