My last Blog the query cache for the Hibernate cache system, and the differences between the list and iterate methods, describe the concept of query caching, and the difference between list and iterate. Readers may notice that the premise of the blog test is that only the query cache is turned on and no level two cache is turned on.
By testing the various scenarios, we can conclude that it is meaningless to only open the query cache and not to open a level two cache . Why do you say that? On a blog can be seen, do not turn on level two cache, iterate () method exists n+1 database query problem, the list method can only hit ID, also need n database query, regardless of the situation, query cache has lost meaning. This blog we turn on the two-level cache, the situation of the previous blog to test, see what will be the effect. hibernate4.1.6 the following configuration in Hibernate.cfg.xml, you can turn on level two cache.
<property name= "Cache.use_second_level_cache" >true</property>
<property name= " Cache.region.factory_class ">org.hibernate.cache.EhCacheRegionFactory</property>
Since both the test code and the database are identical to the previous blog, the only difference is that level two caching is turned on. The code is no longer attached and the results are analyzed directly by the test result.
1.2-time list () Execution result analysis
Testuselist () execution results are as follows:
Hibernate:
Select
student0_.id as id0_,
student0_.name as name0_,
Student0_.age as age0_
from
Student student0_
where
student0_.name= ' zhangsan111 '
list statement tests query cache:hibernate. Student@d6c07[id=1, name=zhangsan111, age=18]
list statement test query cache:hibernate. student@6279d[id=2, name=zhangsan111, age=18]
list statement test query cache:hibernate. Student@12dd76[id=3, name=zhangsan111, age=18]
-------list for a second query------the
list statement to test query cache:hibernate. Student@1e6696c[id=1, name=zhangsan111, age=18]
list statement test query cache:hibernate. student@135133[id=2, name=zhangsan111, age=18]
list statement test query cache:hibernate. Student@381d92[id=3, name=zhangsan111, age=18]
You can find that the list () emits 1 SQL statements for the first time, and the second query does not have access to the database and gets the data directly from the cache.
in the case of the query cache and level two cache, the list will put the obtained ID into the query cache, the key is the SQL statement, the entity object into the level two cache, key is the primary key value of the entity object. The list will first get the ID based on the SQL statement to query the cache, and if not obtained, Issue 1 SQL query statements to query out all required field values.
2.2 times iterate () Analysis of execution results
Testuseiterator () execution results are as follows:
<span style= "FONT-SIZE:14PX;" >hibernate:select student0_.id as col_0_0_ from Student student0_ where Student 0_.name= ' zhangsan111 ' Hibernate:select student0_.id as id0_0_, Student0_.name as name0_0_, s
Tudent0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate. Student@1f7cdc7[id=1, name=zhangsan111, age=18] hibernate:select student0_.id as id0_0_, STUDENT0_.N
Ame as name0_0_, student0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate. student@135133[id=2, name=zhangsan111, age=18] hibernate:select student0_.id as id0_0_, student0_.na
Me as name0_0_, student0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate.
Student@1042fcc[id=3, name=zhangsan111, age=18]-------iterate for a second query------Hibernate:select student0_.id as col_0_0_ from Student student0_ The Where student0_.name= ' zhangsan111 ' iterate statement tests the query cache:hibernate. Student@ad339b[id=1, name=zhangsan111, age=18] Iterate statement test query cache:hibernate. student@14c4d61[id=2, name=zhangsan111, age=18] Iterate statement test query cache:hibernate. Student@6c5356[id=3, name=zhangsan111, age=18]</span>
The first time iterate () query, because there is no data in the query cache and level two cache, need to make n+1 database query, the second query, only one SQL query primary key value, followed by the primary key value from the two-level cache for details.
The iterate method will save the obtained entity object to a level two cache, as to whether the ID will be stored in the query cache, but it is not certain, but it is certain that iterate does not take advantage of the ID value in the query cache, but each time the query re-uses the SQL query to satisfy the condition of the record primary key value. The entity object is then queried from the level two cache based on the primary key.
3. First iterate after list execution result analysis
TestIteratorAndList1 () execution results are as follows:
<span style= "FONT-SIZE:14PX;" >hibernate:select student0_.id as col_0_0_ from Student student0_ where Student 0_.name= ' zhangsan111 ' Hibernate:select student0_.id as id0_0_, Student0_.name as name0_0_, s
Tudent0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate. Student@58cca9[id=1, name=zhangsan111, age=18] hibernate:select student0_.id as id0_0_, student0_.na
Me as name0_0_, student0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate. student@1042fcc[id=2, name=zhangsan111, age=18] hibernate:select student0_.id as id0_0_, STUDENT0_.N
Ame as name0_0_, student0_.age as age0_0_ from Student student0_ where student0_.id=? The Iterate statement tests query cache:hibernate. Student@38d460[id=3, name=zhangsan111, age=18]-------Use iterate for the first time, use the list query for the second time------hibernate:select student0_.id as id0_, student0_.name as name0_ , student0_.age as age0_ from Student student0_ where student0_.name= ' zhangsan111 ' list language Sentence test query cache:hibernate. Student@7efa96[id=1, name=zhangsan111, age=18] List statement test query cache:hibernate. student@75c78d[id=2, name=zhangsan111, age=18] List statement test query cache:hibernate. Student@3acc67[id=3, name=zhangsan111, age=18]</span>
As you can see, iterate's cache information is not helpful for lists. The list emits 1 SQL and queries all required field values from the database. Here you can see:
The iterate method does not place the primary key value in the query cache, because if it is put into the query cache, there is no need for the list to issue the SQL query again.
4. First list after iterate execution result analysis
TestIteratorAndList2 () execution results are as follows:
<span style= "FONT-SIZE:14PX;" >hibernate:
Select
student0_.id as id0_,
student0_.name as name0_,
student0_.age as age0_
From
Student student0_
where
student0_.name= ' zhangsan111 '
list statement tests query cache:hibernate. Student@67c1a6[id=1, name=zhangsan111, age=18]
list statement test query cache:hibernate. student@12f41a5[id=2, name=zhangsan111, age=18]
list statement test query cache:hibernate. Student@17ebe66[id=3, name=zhangsan111, age=18]
-------for the first time using list, the second time using iterate query------
Hibernate:
Select
student0_.id as col_0_0_
from
Student student0_
where
student0_.name= ' The zhangsan111 '
Iterate statement tests query cache:hibernate. Student@feecca[id=1, name=zhangsan111, age=18]
Iterate statement test query cache:hibernate. student@6bd9e0[id=2, name=zhangsan111, age=18]
Iterate statement test query cache:hibernate. Student@11710be[id=3, name=zhangsan111, age=18]</span>
The conclusion that can be reached here is similar to the 2-time list () query. The only difference is:
iterate each query emits an SQL statement that queries the ID of the entity object.
5. Summary
By turning on the query cache and level two cache, the same SQL query can directly use the ID in the query cache and the entity object in level two cache, which can effectively reduce the duplicate database query and improve the query efficiency. That is, it is meaningful to open the query cache and level two cache at the same time, and it is the best configuration to actually use hibernate.
Further, we can also see the difference between the list and the iterate method. list () puts the ID of the entity object into the query cache, puts the entity object itself into a level two cache; iterate does not place the ID of the entity object in the query cache, but the entity object itself into a level two cache.
If the second query can hit the case : The list does not need to query the database at all, you can get the ID from the query cache, and then get the entity object from the level two cache; Iterate must issue a SQL that checks the ID, and then go to the level two cache to get the entity object.
The relationship between Hibernate's level two cache and query cache has been completed, and the list and iterate differences have been tested. Hope to be helpful to everyone, if wrong, welcome Daniel to correct me.