The difference between hibernate and iterate the list


One. Hibernate of the SQL query N + 1 problem ------- Iterate

ibernate use session.createQuery (hql) query data when there are two ways to search:

A query only once, all the data to be queried both check out, direct access to the data behind it;

      Obtaining: session.createQuery (hql) .list ().

      The disposable loading of data, low efficiency, for example, product information display. It applies to all information display.

2, the other is a query to the primary key check out, when needed later data, an access to data in a primary key.

       Obtaining: session.createQuery (hql) .iterate ().

      News list for the show. When you need to view the news content, in particular content to query data based on the primary key.

      It applies to only one or a few show a message.

Second, tell us about the java caching system JCS (java Cache System)
 1, JCS (Java Caching System) is an object Cache, Java objects that can be cached to improve the efficiency of those accessing a high frequency of access Java objects . JCS accordance uniquely identifies the object to access the object, such an object can be accessed in accordance hashCode.
 2, for Hibernate, the JCS can be used to cache query results, so when the next visit to the same data, you need to take the database, taken directly from the JCS, to accelerate query speed. 
 3, when using Hibernate List or Iterator way to read the data for the first time, the JCS is empty, then either List mode or Iterator way will be to check out the filling inside JCS persistent objects,
 such as: select c from Cat as c 
  and select c.id, c.name from Cat as c HQL statement that is not configured PO, and therefore not to fill JCS. 
 4, from the region JCS data:
  fetch data from JCS in accordance uniquely identifies the object to be accessed, and for PO persistent object, the unique identifier is a primary key, Hibernate must first obtain the primary key list, and then one by one according to the master key list determination, to see whether this persistent object or in a database, if the inside JCS, then taken in accordance with the master key inside JCS, if the database, then send sql taken.
 5, here to tell us about the Iterator can use the JCS, while List can not. Above that, before using JCS, must first obtain the primary key of persistent objects in order to get inside the JCS persistent objects, to obtain a list of the primary key must be made to the database, this step is no way to buffer.

 Two, list and iterate query data in different forms:

1, Hibernate Iterator query itself is divided into two steps: the problem of the presence of N + 1
  ==> select id from cat //一条
  ==> select * from cat where id = ?  //n条 
  Analysis: The first step, the database to fetch the primary key list, 
the second step, according to a primary key of a data fetch. JCS If not, then removed from the database n records need n + 1 times sql query,
 if you use the JCS, JCS he will first take a look inside, look for persistent objects in accordance with the primary key, if there, take a direct out with, if not, then I had to go get the database before sending it to fill JCS go inside. 2, and Hibernate List mode: ==> select * from cat It is a simple JDBC package, once sql put all the data are taken out, it did not take the first primary key as Iterator, then take the data, and therefore can not use List JCS.
List may be filled but the data extracted from the database to the JCS inside.
 When using a list of query data again, still read the data to be sent sql database, proof list () method does not use cache

The first step, the database to fetch the primary key list, the second step, according to a primary key of a data fetch. Of course, we can now see, under Iterator mode if no JCS, then remove the n records from the database you need n + 1 times sql query, this is a very terrible thing, so if without the use of JCS in you must also pick up a lot of data, you should avoid using Iterator.  

The Iterator first sql primary key is to take the list, this time consumption is very small, if you use the JCS, then every time the query is still to be inevitable to send a sql: select id from cat to fetch a list of primary key, then it ? Then Iterator will not be so stupid, he will first take a look inside the JCS, look for persistent objects in accordance with the primary key, if you have a direct out with, if not, then had to go get the database, then put it to fill JCS to go inside.  

Later it can be seen, JCS is somewhat similar to the Simple Object database in memory, the first Iterator once sql fetch the primary key list is a must to go to the database inside taken, made a primary key that key, Iterator will first try to open the JCS that the lock, hit the open directly into, if not open, we had to open the lock of the database.  

JDBC and Hibernate List is a simple way to package a sql put all the data is taken out, it did not take the first primary key as Iterator, then take the data, and therefore can not use List JCS. List may be filled but the data extracted from the database to the JCS inside.  

The best way: first visit to use List, quickly fill JCS, after access using Iterator, make full use of JCS. 

mikeho write:  

That is how JCS Database and keep pace?  

robbin write:  

jcs.default.elementattributes.MaxLifeSeconds = 240 (maximum buffer time)  

Set aside time out to do, plus you can also clear JCS cache inside the program 

Three, list and return the object iterate analysis:
 List return () is an entity object, and the object iterate () returns a proxy object.

https://blog.csdn.net/liumuqi110/article/details/4625055

Four, list and iterate using the cache case

1, iterate queries the session cache, level 2 cache, the list will only use the query cache (if the query cache is turned on, then)
 2, when the query cache is turned on, the impact on iterate and list query of
  the query cache: The query cache is already open , the cache is query itself to sql hql generated, together with parameters, paging information such as a key value, rather than the result of the query results .query the session is placed in the cache and the secondary cache, the query and . the cache is differentiated
  configuration query cache: open the query cache: 1, is provided in the config hibernate.cache.use_query_cache = true, 2, query.setCacheable (true) created after the query;
 
 the following is a cache query to open with a list of several scenarios:
  1:

  Query q1 = s1.createQuery("from com.test.hb.Hbtest h where h.id<3");
  q1.setCacheable(true);

  Query q2 = s1.createQuery("from com.test.hb.Hbtest h where h.id=1");
  q2.setCacheable(true);

  Logically, the first query query the value should contain the second query, but the second query does not use cache query, but will re-query the database.

  2:
  Query q1 = s1.createQuery("from com.test.hb.Hbtest h where h.id=1");
  q1.setCacheable(true);

  Query q2 = s1.createQuery("from com.test.hb.Hbtest h where h.id=1");
  q2.setCacheable(true);

  Two identical query, the second query will use cache query, and will not re-query the database.

  3:
  Query q1 = s1.createQuery("from com.test.hb.Hbtest h where h.id=?").setInteger(0, 1);
  q1.setCacheable(true);

  Query q2 = s1.createQuery("from com.test.hb.Hbtest h where h.id=?").setInteger(0, 1);
  q2.setCacheable(true);

  Two query is the same, the second query will use cache query, and will not re-query the database.

  4,
  Query q1 = s1.createQuery("from com.test.hb.Hbtest h where h.id=?").setInteger(0, 1);
  q1.setCacheable(true);

  Query q2 = s1.createQuery("from com.test.hb.Hbtest h where h.id=?").setInteger(0, 2);
  q2.setCacheable(true);

  Logically, two query is not the same, the second query does not use cache query, but will re-query the database.

 

 query the cache after opening with iterator scene:
  . The query cache does not cache the first time for the ID of SQL, iterator back when you can say that will query the cache has no effect on the decision cache access iterator According session in the database .
  ** If the cache is a native SQL, two query sql statement must be the same (including case) as the following example accesses the database twice.
  local sql:
  Query q1 = s1.createSQLQuery ( "Val from the SELECT WHERE h.id. 1 = H Hbtest ");
  Query s1.createSQLQuery Q2 = (" SELECT from Hbtest Val WHERE h.id = H. 1 ");

  For HQL does not, hibernate HQL will be translated into SQL, this process will be 'identified' is not the same SQL. The following example is only accessible once the database.
  Query s1.createQuery Ql = ( "from com.test WHERE h1.id. 1 = h1 of .hb.Hbtest ");
  Query s1.createQuery Q2 = (" WHERE h.id from com.test.hb.Hbtest = H. 1 ");

五、iterate和list的使用情况:
 iterate:
 s1 = sessionFactory.openSession();
 Query q = s1.createQuery("from com.test.hb.Hbtest h where h.id<2");
 Iterator itr = q.Iterator(); 
 while(itr.hasNext()){
  Hbtest tbo = (Hbtest)itr.next();
 }

 list:
 iterate:
 s2 = sessionFactory.openSession();
 Query q = s2.createQuery("from com.test.hb.Hbtest h where h.id<2");
 List ls = q.list(); 
 int i=0;
 while(i<ls。size()){
  Hbtest tbo = (Hbtest)ls。get(i);
  i++;
 }

 Using a combination of both forms:
 S1 = sessionFactory.openSession ();
 Query s1.createQuery Q = ( "WHERE h.id from com.test.hb.Hbtest H <2");
 List q.list L = () ; 
 the Iterator l.iterator ITR = ();
 the while (itr.hasNext ()) {
     Hbtest TBO = (Hbtest) itr.next ();
 }
 List () method will be taken once all the results set object, and he will be based on the query the result initialize all of the result set objects. If it takes up a lot of memory when the result set is very large, and even cause memory overflow occurs.
 iterator () method will not once initialized in the implementation of all of the objects, but to initialize the object based on visits to the result set. In the first visit you can control the number of objects in the cache to avoid consuming too much cache, cause memory overflow condition.

 Original link: http://www.linuxidc.com/Linux/2012-01/52003.htm

 

Guess you like

Origin www.cnblogs.com/lukelook/p/11076747.html