On Hibernate n + 1 problem

Stored in the session cache are interrelated object graphs. By default, when hibernate loads the customer object from the database, all associated order objects are loaded at the same time. Taking the customer and order classes as examples, assume the customer of the orders table_ Null allowed for ID foreign key

The find() method of the following session is used to retrieve all customer objects from the database:

List customerLists=session. find("from Customer as c");

When running the find () method above, hibernate will first query all records in the customers table, and then query the records with reference relationship in the orders table according to the ID of each record. Hibernate will execute the following select statements in turn:

Through the above five select statements, hibernate finally loads four customer objects and five order objects, forming an associated object graph in memory

Hibernate uses the default immediate retrieval strategy when retrieving the order object associated with customer. This retrieval strategy has two shortcomings:

(1) The number of select statements is too large, which requires frequent access to the database, which will affect the retrieval performance. If you need to query n customer objects, you must execute the select query statement n + 1 times. This is the classic n + 1 select query problem. This retrieval strategy does not make use of the connecting query function of SQL. For example, the above five select statements can be used through the following one select statement Sentence to complete:

The above select statement uses the left outer join query function of SQL, which can query all records of customers table and matching records of orders table in one select statement.

(2) When the application logic only needs to access the customer object instead of the order object, loading the order object is completely redundant. These redundant order objects waste a lot of memory space.

In order to solve the above problems, hibernate provides two other retrieval strategies: delayed retrieval strategy and urgent left external connection retrieval strategy. The delayed retrieval strategy can avoid redundant loading of associated objects that the application does not need to access. The urgent left external connection retrieval strategy makes full use of the external connection query function of SQL and can reduce the number of select statements.

Performance must be considered for database access. After setting the 1-to-many relationship, the legendary n + 1 problem will appear in the query.

1) 1 to many, if n objects are found on the 1 side, then the set associated with n objects needs to be taken out, so the original SQL query becomes n + 1

2) Many to one. If M objects are obtained from the multi-party query, the objects of party 1 corresponding to m objects will also be taken out and become m + 1

How to solve the N + 1 problem?

1) Lazy = true. Hibernate 3 has defaulted to lazy = true; When lazy = true, the associated object will not be queried immediately. The query action will occur only when the associated object (access its properties, non ID fields) is required.

2) For L2 cache, when objects are updated, deleted and added much less than queries, the application of L2 cache will not be afraid of the N + 1 problem, because even if the first query is very slow, the direct cache hit is very fast. Different solutions, different ideas, but the second just makes use of N + 1.

3) Of course, you can also set fetch = join (annotation: @ manytoone() @ fetch (fetchmode. Join))

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>