Hibernate performance optimization solution

Source: Internet
Author: User

 

Hibernate Performance Tuning (reprinted by Robbin fan)

I. Inverse =?

Inverse = false (default)

For one-to-one Association

Parent. getchildren (). Add (child) // insert child

Parent. getchildren (). Delete (child) // delete child

Inverse = true

Used for Bidirectional one-to-one Association

Child. setparent (parent); Session. Save (child) // insert child

Session. Delete (child)

In a layered structure system

The crud encapsulation of parentdao and childdao results in the persistence of objects directly through the session interface, but seldom the accessibility of objects through association.

II. One-to-one relationship

Unidirectional or bidirectional?

The hitting operation of parent. getchildren (). Add (child) on the set will lead to the initialization of the lazy set. This operation should be avoided if no second-level cache is configured for the set.

Select * from child where parent_id = xxx;

Performance tips:

1. Avoid unidirectional association in general, and try to use bidirectional association as much as possible.

2. Use bidirectional Association, inverse = "true"

3. In the layered structure, the session is used to directly persist objects through the DaO interface, avoiding accessibility persistence through associations.

3. Relationship-to-one

One-way foreign-to-one expresses the foreign key storage party

Flexible use of parallel-to-one can avoid unnecessary performance problems

Values-to-one indicates 0 .. n: 1. values can be 0, 1, or N. That is, values-to-one can express one-to-many, one-to-one, and many-to-one relationships.

Therefore, you can configure a two-way sequence-to-one relationship, for example:

1. What is the relationship between a table with four people playing mahjong and the mahjong seats and those playing mahjong? Is the relationship between two-way parallel-to-one

4. One-to-one

Associate with a primary key

Split a large table into multiple small tables.

For example, split large fields separately to Improve the Performance of database operations.

Hibernate's one-to-one seems to be unable to lazy, and must use bytecode Enhancement

5. Set list/bag/Set

One-to-least

1. Index column needs to be maintained for list and cannot be used for Bidirectional Association. It must be inverse = "false" and used with caution in rare occasions.

2. There is no semantic difference between bag and set.

3. I personally prefer to use bag

Copying-to-Sequence

1. There are differences between bag and set semantics.

2. Set is recommended.

Sat. Filter Sets

1. Children = session. createfilter (parent. getchildren (), "where this. age> 5 and this. age <10"). List ()

In the case of a very large set of one-to-multiple associations, it is particularly suitable for pagination of a large set:

Session. createfilter (parent. getchildren (), ""). setfirstresult (0). setmaxresults (10). List ();

Using Super. getsession (). createfilter (,) in hibernate (,)

7. Implicit polymorphism in the inheritance relationship

Hql: From Object

1. query all database tables

2. polymorphism = "implicit" (default) extracts all the current object and all the inherited subclasses of the object at a time.

3. polymorphism = "explicit". Only the current query object is retrieved.

8. Hibernate second-level cache

The famous n + 1 problem: from child. Then, the parent class information of each subclass is displayed on the page, which leads to N queries to the parent table:

Select * from parent where id =?

.......................

Select * from parent where id =?

Solution

1. Eager fetch

2. Secondary Cache

9. Relationship between inverse and second-level cache

When the set cache is used:

1. Inverse = "false", which is operated by parent. getchildren (). hibernate maintains the set cache.

2. Inverse = "true", operate directly on child, and fail to maintain the set cache! Dirty cache data

3. Bidirectional Association. If inverse = "true", you should avoid using the set cache.

10. Hibernate second-level cache is a magic weapon to improve Web Application Performance

For OLTP web applications, because the application server can perform horizontal cluster scaling, the final system bottleneck is always unable to escape database access;

Which framework can minimize database access and reduce database access pressure, and which framework provides higher performance? database cache policies:

1. Object cache: fine-grained, transparent access to table records, which can greatly improve the performance of Web applications without changing program code. Object cache is a magic weapon to win over Orm.

2. The advantages and disadvantages of Object Caching depend on the implementation level of the Framework. Hibernate is the most powerful open-source ORM for known object caching.

3. query cache: coarse granularity, applicable to scenarios with low real-time data requirements for query result sets

11. Application scenarios determine the system architecture

1. requires an ORM

Hibernate or ibatis?

Ii. Using ORM determines the database design

Hibernate:

Users tend to adopt fine-grained design and object-oriented design. They split large tables into small tables with multiple associations to eliminate redundant columns and improve performance through second-level cache (DBAs tend to avoid association relationships, however, the ORM cache will break through the performance bottleneck of the association relationship). The performance bottleneck of hibernate does not lie in the association relationship, but in the operations of large tables.

Ibatis:

Tends to adopt coarse-grained design and link-oriented design. tables should be merged as much as possible to eliminate association relationships through table column redundancy. There is no effective caching method. The performance bottleneck of ibatis lies not in large table operations, but in associations.

Summary:

Performance tips

1. Use bidirectional one-to-Multiple Association instead of unidirectional one-to-Multiple Association

2. flexible use of one-way multi-to-one Association

3. Replace multiple-to-one instead of one-to-one

4. Configure the object cache without using the set Cache

5. Use bag for one-to-multiple sets and set for multiple-to-multiple sets

6. Use explicit polymorphism for inheritance classes

7. The number of table fields should be small, and the table association should not be too large. The level-2 cache is supported.

Recently, I began to pay attention to the performance issues of hibernate in the project. I hope I can take the time to learn about the performance optimization of hiberante. I am mainly studying database connection pool technology, Hibernate second-level cache, and hibernate configuration optimization!

1. Link:

Common Association: A join table is not included, that is, an intermediate table, for example:

Create Table person (personid bigint not null primary key, addressid bigint not null)

Create Table address (addressid bigint not null primary key)

That is, there will not be a relational table such:

Create Table person (personid bigint not null primary key)

Create Table address (addressid bigint not null primary key)

Create Table personaddress (personid bigint not null, ddressid bigint not null primary key)

Unidirectional sequence-to-one association is the most common, while unidirectional one-to-sequence is not.

2. Inner join (internal join)

Left (outer) join (left Outer Join)

Right (outer) join (right Outer Join)

Full join (full join, not commonly used)

3. tips:

Number of statistical results:

(Integer) Session. iterator ("select count (*) from..."). Next (). intvalue ();

Sort by the size of a set:

Select User. ID, user. Name

From user as user. Name

Left join user. Messages msg

Group by user. ID, user. Name

Having count (MSG)> = 1

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.