Hibernate queries slow down drastically after an entity is loaded in the session

后端 未结 3 1294
甜味超标
甜味超标 2020-12-28 21:03

I\'m using Hibernate EntityManager, and am experiencing a weird slowdown in my Hibernate queries. Take a look at this code:

public void testQuerySpeed() {
           


        
3条回答
  •  旧巷少年郎
    2020-12-28 21:55

    Your executing a native query which is not saying anything about what it will be touching and thus Hibernate will have to (for consistency sake) flush() against all data from all tables it knows about (and your single find() might have fetched more than just one object so it might not be a trivial operation).

    To optimize this make sure that you use the SQLQuery.add* methods to define what the query is actually doing. In this case query.addSynchronizedQuerySpace("bogustablename") should do the trick about telling Hibernate that this query is just scalar data from no specific table.

提交回复
热议问题