Hibernate optimization scheme

Source: Internet
Author: User
Tags what sql

 1. Batch modification and Deletion
In hibernate 2, if you want to modify or delete any data, you must first query the data to be modified or deleted, then perform operations on the data. There is no problem in using this processing method when the data volume is small, but the following problems may occur when you need to process a large amount of data:

It occupies a large amount of memory.
The update/delete statement needs to be executed multiple times, and only one data record can be processed at a time.
The above two problems will seriously affect the system performance. Therefore, hql statements used to update or delete data in batches are introduced in hibernate 3. In this way, developers can update or delete multiple records at a time, instead of modifying or deleting records at a time.

To delete all user objects (that is, records in the table corresponding to the user object), you can directly use the following hql statement:
Delete user
When executing this hql statement, you need to call the executeupdate () method of the query object. The specific example is as follows:
String hql = "delete user ";
Query query = session. createquery (hql );
Int sizeappsquery.exe cuteupdate ();
When using this method to modify and delete data, the performance of direct JDBC is almost the same, which is the correct method recommended.
If the hql statement cannot be used to modify a large amount of data, that is to say, the memory overflow problem during batch insertion is also encountered when you can only retrieve and modify the data, therefore, the preceding processing method must be used for similar processing.

  Ii. use SQL to Perform Batch operations
When performing batch insert, modify, and delete operations, using JDBC directly to execute the original SQL statement will undoubtedly achieve the best performance, this is because the following content is omitted or simplified during processing:
● Convert hql statements to SQL statements.
● Java object initialization.
● Cache processing of Java objects.
However, when using JDBC to execute SQL statements directly, the most important problem is to process the cached Java objects. Because the underlying data modification method cannot notify the cache to perform corresponding update operations to ensure that the cached objects are consistent with the data in the database.

  Iii. Improve database query performance
The improvement of database query performance also involves various stages of development. Selecting the correct query method in development is undoubtedly the most basic and simplest.
 1. SQL statement Optimization
Using correct SQL statements can greatly improve the system query performance. The performance gap between SQL statements that use different methods to obtain the same data may be huge.
Since hibernate encapsulates JDBC, SQL statements are automatically generated by hibernate dynamically. There are two ways for hibernate to generate SQL statements: one is to generate an hql statement compiled by a developer, the other is to automatically generate corresponding SQL statements based on developers' access to the associated objects.

What SQL statements can be used to achieve better performance must be processed based on the database structure and the specific circumstances of the data to be obtained. After determining the SQL statement to be executed, you can influence the SQL statement generated by hibernate in the following three aspects:

Hql statement writing method.
The query method used for query.
The crawling policy used for object Association.
 2. Use the correct query method
As mentioned earlier, there are two basic methods for executing the data query function: Get () method and load () method for a single Persistent Object, the other is the list () method and iterator () method of the query object. In development, you should select the correct method based on different situations.

The difference between the get () method and the load () method lies in the use of the second-level cache. The load () method uses the second-level cache, while the get () method directly queries the database if the first-level cache is not found. In use, it is best to use the load () method to query objects that use the second-level cache to make full use of the second-level cache to improve the retrieval efficiency.

The differences between the List () method and the iterator () method can be compared from the following aspects.
   Different queries are executed.
The List () method is used to directly run the query statement required for the query results, while the iterator () method is used to first execute the query to obtain the Object ID, then retrieve the object to be queried based on each ID value. Therefore, for list () queries, only one SQL statement is executed, and for iterator () for method query, you may need to execute n + 1 SQL statement (n is the number of records in the result set ).

The iterator () method only executes n + 1 pieces of data. The number of SQL statements executed depends on the cache and the access to the result set.
  Cache Usage
The List () method can only use the query cache in the second-level cache, but cannot use the second-level cache to cache a single object (but the queried object will be placed in the second-level cache ). Therefore, the cache mechanism cannot be used to improve the query efficiency unless the same query operation is repeated.

The iterator () method makes full use of the second-level cache. When retrieving an object by ID, it first goes to the cache for search. The corresponding query statement is executed only when the object cannot be found. Therefore, the existence of objects in the cache will affect the number of SQL statements executed.

   Different Processing Methods for result sets
The List () method obtains all result set objects at a time and initializes all result set Objects Based on the query results. This will inevitably occupy a large amount of memory when the result set is very large, and may even cause memory overflow.

The iterator () method does not initialize all objects at a time during execution, but initializes Objects Based on access to the result set. Therefore, you can control the number of objects in the cache during access to avoid occupying too much cache, resulting in memory overflow. Another advantage of using the iterator () method is that if you only need some records in the result set, the result objects that are not used will not be initialized at all. Therefore, access to the result set is also a factor in the number of database SQL statements executed when the iterator () method is called.

Therefore, when using a query object to perform data queries, you should consider the methods used to perform database query operations from the above aspects.
  4. Use the correct capture policy
The so-called fetching strategy refers to the policy that hibernate acquires associated objects when an application needs to use the association relationship to obtain objects. The capture policy can be declared in the metadata of the O/R ing, or in a specific hql or condition query.

Hibernate 3 defines the following capture policies.
Join fetching)
Connection crawling means that hibernate uses external connections in the SELECT statement to obtain the associated object when obtaining the associated object.
Select fetching)
Query crawling means that hibernate uses another SELECT statement to capture the associated objects of the current object. This is also used to execute database queries by using foreign keys. The difference between the SELECT statement and the join capture statement is that the SELECT statement is not executed immediately, but is executed only when the associated object is accessed.

Subselect fetching)
Subquery crawling is also the way that hibernate uses another SELECT statement to capture the associated objects of the current object. The difference between Query capture and query capture is that the SELECT statement is a subquery, rather than an external connection.

Batch fetching)
Batch crawling is an optimization of query crawling. It uses a single SELECT statement to manage objects in batches based on the list of primary keys or foreign keys.
The above describes the crawling policy provided by hibernate 3, that is, the means to capture associated objects. In order to improve the system performance, the following options are available when the associated objects are captured.
Capture now (Immediate fetching)
Immediate capturing means that when a host object is loaded, the object associated with it will be loaded immediately.
Lazy collection fetching)
Delayed collection capturing means that when loading a host object, it does not immediately load the object it is associated with, but crawls the associated object only when the application accesses the associated object. This is the default behavior of the object associated with the set.
Lazy proxy fetching)
Latency proxy crawling means that when a single-value associated object is returned, the object is not captured during the get operation, but is not captured until a method is called.
Lazy attribute fetching)
Delayed attribute loading refers to crawling associated objects only when the associated objects are accessed.
This article introduces how to capture and capture the associated objects provided by hibernate. These two factors will affect the crawling behavior of hibernate, the most important thing is to be clear that the effects of these two aspects are different. Do not confuse these two factors. In development, select the correct crawling policy and the appropriate capturing time based on the actual situation.

  Selection of capture time
In hibernate 3, Association of the set type uses the capture time of delayed collection loading by default, by default, the association that returns the single-value type will use the capture time of the delayed proxy capture.
It is rarely used to capture data immediately during development, because it may cause unnecessary database operations and affect system performance. This crawling time is only possible when the host object and associated object are always Accessed at the same time. In addition, you can use the instant join capture function to reduce the number of SQL statements to be queried through external connections. Therefore, it will also be used in some special cases.

However, delayed loading faces another problem. If the associated object is not instantiated before the session is closed, an exception will be thrown when accessing the associated object. The processing method is to complete access to the associated object before the transaction is committed.

Therefore, latency is usually used to capture associated objects. Because each instant capture will cause the associated object to be instantly instantiated, too many instant captures of the association will lead to a large number of objects to be instantiated, thus occupying excessive memory resources.

  Capture Policy Selection
The selection of crawling policies will affect the way associated objects are captured, that is, the SQL statement executed when the associated objects are captured. This should be selected based on the actual business needs, the number of data and the structure of the database.
Note that the appropriate crawling policy is usually specified for each query during query execution. The method for specifying a crawling policy is as follows:
User user = (User) Session. createcriteria (user. Class)
. Setfetchmode ("Permissions", fetchmode. Join)
. Add (restrictions. ideq (userid ))
. Uniqueresult ();
  5. Summary of query performance improvement
This section describes how to improve query performance. The key is how to optimize SQL statements to improve system query performance. The query method and capture policy also affect the system performance by executing the query method and the number of SQL statements. These are the basic skills that developers should possess to avoid system performance degradation due to improper development.

In performance adjustment, in addition to the preceding SQL statement execution factors, Cache Usage also affects the system performance. In general, the use of cache increases the performance of system queries, and reduces the performance of system addition, modification, and deletion operations (because cache synchronization is required ). Therefore, developers should be able to correctly use the effective cache to improve the performance of data queries, and avoid misuse of the cache resulting in lower system performance. When using the cache, you should also pay attention to adjusting your search policy and query method. These three can work together to achieve optimal performance.

In addition, the transaction Usage Policy also affects the system performance.

 

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.