Considerations on the optimization of Hibernate program performance

Source: Internet
Author: User
Program | performance | optimization

This article according to the Hibernate help document, some network books and the project experience reorganization, only provides the main point and the idea, the concrete method may leave a message to discuss, or find some more detailed and pertinent information.

Initial use of Hibernate may have encountered performance problems, the same function, with Hibernate and JDBC performance of more than 10 times times the difference is normal, if not adjusted early, it is likely to affect the progress of the entire project.

In general, the main considerations for Hibernate performance tuning are as follows:

Ø Database Design Adjustment

ØHQL optimization

Proper use of øapi (such as selecting different sets and query APIs based on different business types)

Ø main configuration parameters (log, query cache, fetch_size, batch_size, etc.)

Ø mapping file Optimization (ID generation strategy, Level two cache, delay loading, association optimization)

Ø first-level cache management

Ø for level Two caching, there are many unique strategies

Ø transaction control strategy.

1. Database design

A) Reduce the complexity of the association

b) Try not to use a joint primary key

c) The generation mechanism of IDs, the mechanisms provided by different databases are not exactly the same

(d) Appropriate redundant data, not excessive pursuit of high paradigm

2, HQL optimization

HQL If you put aside its association with some caching mechanisms of the hibernate itself, HQL's optimization techniques, like normal SQL optimization techniques, can easily find some experience on the web.

3. Main configuration

A the query cache, unlike the cache below, is a cache of HQL statements that can take advantage of cached data when the exact same statement is executed again. However, query caching can be counterproductive in a trading system where the frequency of data changes is not as high as it is likely to be: it consumes a lot of system resources, but it's hard to get in handy.

b) Fetch_size, similar to the parameters of JDBC, the parameters are not the greater the better, but should be based on business characteristics to set

c) Batch_size Ibid.

D production system, remember to turn off the SQL statement printing.

4, caching

A database-level caching: This level of caching is most efficient and secure, but different tiers of database management are not the same, for example, in Oracle, you can specify that the entire table be placed in the cache while the table is being built.

(b) Session caching: In a Hibernate session effective, this level of caching is not strong, most of the Hibernate automatic management, but it provides a way to clear the cache, which in the mass increase/update operation is effective. For example, if you add 100,000 records at the same time, and you are likely to find Outofmemeroy exceptions, you may need to manually clear this level of caching: Session.evict and Session.clear

C Application caching: In a sessionfactory is effective, so it is the most important to optimize, therefore, the various strategies are also considered more, before putting data into this level of caching, you need to consider some prerequisites:

I. Data will not be modified by a third party (for example, is there another application that is modifying the data?)

II. Not too much data

III. Data will not be updated frequently (otherwise using cache may backfire)

IV. Data will be frequently queried

V. Data is not a key data (e.g., money, security, etc.).

There are several forms of caching that can be configured in the mapping file: read-only (read-only, static data/historical data that is rarely changed), Nonstrict-read-write,read-write (more general form, efficiency general), transactional (JTA, and less supported cache products)

d) distributed caching: The same as C, only the selection of the cache products, in the current hibernate is not much to choose from, Oscache, JBoss Cache, most of the current projects, their use of the cluster (especially the key trading system) are conservative attitude. In a clustered environment, only database-level caching is the safest.

5, Delay loading

A) Entity deferred loading: By using dynamic proxy implementations

(b) Set delay loading: Provides support for this by implementing its own set/list,hibernate

C) Property deferred loading:

6, Method selection

A to accomplish the same thing, Hibernate offers some options to choose from, but the specifics of how to use it may be affected by performance/code. Shows that a return of 100,000 records (LIST/SET/BAG/MAP, etc.) at once is likely to result in insufficient memory, and if the result set based on a cursor (scrollableresults) or iterator is used, there is no such problem.

(b) The Load/get method of the session, which uses level two caching and the latter is not used.

C Query and List/iterator, if you take a closer look at them, you may find a lot of interesting situations, the main difference (if you use Spring, the corresponding Find,iterator method in Hibernatetemplate):

I. The list can only take advantage of query caching (but the query cache does little in the trading system) and cannot take advantage of a single entity in the level two cache, but the object identified by list is written to level two cache, but it typically generates fewer execute SQL statements, and many cases are (unrelated).

Ii. iterator can use level two cache, for a query statement, it will first find all eligible records from the database ID, and then through the ID to cache, for the cache does not record, and then construct the statement from the database to find out, so it is easy to know, If there are no records in the cache that match the criteria, using iterator results in a n+1 SQL statement (n is the number of records that match the criteria)

Iii. through iterator, with the cache management API, in the mass data query can be a good solution to memory problems, such as:

while (It.hasnext ()) {

Youobject object = (youobject) it.next ();

Session.evict (Youobject);

Sessionfactory.evice (Youobject.class, Youobject.getid ());

}

If you use the list method, you may be outofmemory wrong.

Iv. through the instructions above, I think you should know how to use these two methods.

7, the selection of sets

In the Hibernate 3.1 Document of "19.5." Understanding Collection Performance "is described in detail.

8. Transaction control

The main issues that affect performance include: The choice of transaction mode, the level of transaction isolation and the selection of locks

A The choice of transactions: If you do not involve multiple transaction manager transactions, you do not need to use JTA, only JDBC transaction control.

(b) Transaction ISOLATION LEVEL: see Standard SQL Transaction Isolation level

c) Lock selection: Pessimistic lock (generally implemented by the specific transaction manager), for long transaction efficiency is low, but safe. Optimistic locks (typically implemented at the application level), such as the version field that can be defined in hibernate, it is obvious that optimistic locks fail if there is more than one application operating data and these applications are not using the same optimistic locking mechanism. Therefore, there should be different strategies for different data, as in many cases, many times we find a balance between efficiency and security/accuracy, however, optimization is not a purely technical issue and you should have a good understanding of your application and business characteristics.

9. Bulk operation

Even with JDBC, there is a significant difference between batch and batch efficiency when large data updates are made. We can set the Batch_size to support bulk operations.

For example, to bulk delete the object in a table, such as "Delete account", the statement, will find that Hibernate found all account ID, and then delete, this is mainly to maintain the level two cache, so the efficiency is certainly not high, Bulk Delete/update was added to subsequent releases, but this did not resolve the cache maintenance issue. In other words, because of the two-level cache maintenance problems, Hibernate's bulk operation efficiency is not satisfactory!

As can be seen from many previous points, we often find a balance between efficiency and safety/accuracy. In any case, optimization is not a pure technology problem, you should have a good understanding of your application and business characteristics, in general, the optimization plan should be in the framework of the design period is basically determined, or may lead to unnecessary rework, Cause the project to be postponed, and as the architect and project manager, faced with the developer's possible complaints, we have little control over the changes in user requirements, but the technical/architectural risks should be realized at an early stage and the relevant countermeasures should be developed.

Also note that the application layer of caching is only icing on the cake, never take it as a lifeline, the application of the foundation (database design, algorithm, efficient operation statements, the appropriate API selection, etc.) is the most important.



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.