Hibernate is 1000 times slower than sql query

后端 未结 2 484
灰色年华
灰色年华 2020-12-16 22:30

I have this setup

@Table(name =\"A\")
EntityA {
    Long ID;
    List children;
}

@Table(name =\"B\")
EntityB {
    Long ID;
    EntityA pare         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-16 23:10

    I finally managed to get to the bottom of this. The problem was being caused by Hibernate setting the parameters separately from the actual SQL query that involved subqueries. So native SQL or not, the performance will be slow if this is done. For example this will be slow:

    String sql = some sql that has named parameter = :value
    SQLQuery sqlQuery = session.createSQLQuery(sql);
    sqlQuery.setParameter ("value", someValue);
    List list = (List)sqlQuery.list();
    

    And this will be fast

    String sql = some native sql where parameter = 'actualValue'
    SQLQuery sqlQuery = session.createSQLQuery(sql);
    List list = (List)sqlQuery.list();
    

    It seems that for some reason with letting Hibernate take care of the parameters it ends up getting stuck in the resultSet fetching. This is probably because the underlying query on the database is taking much longer being parameterized. I ended up writing the equivalent of Hibernate Criteria and Restrictions code that sets the parameters directly as above.

提交回复
热议问题