Slow performance on Hibernate + Java but fast when I use TOAD with the same native Oracle query

前端 未结 3 1343
难免孤独
难免孤独 2021-01-01 21:07

I\'ve detected a performance problem with hibernate and native queries on Oracle. When I execute a complex SQL query with several parameters on TOAD I get the result in mili

相关标签:
3条回答
  • 2021-01-01 21:30

    <property name = "hibernate.temp.use_jdbc_metadata_defaults">false</property>

    Add this to your hibernate.cfg.xml or update your application properties file.

    0 讨论(0)
  • 2021-01-01 21:41

    My answer to you is:

    Remove all bind parameters and use StatelessSession instead of Session

    Use SQLQuery instead of query with full SQL including parameter values

    StatelessSession session = sessionFactory.openStatelessSession();
    

    I had similar problem and till I get better solution,this is what I managed to make it work. See Hibernate parameterized sql query slow and active oracle sessions

    0 讨论(0)
  • 2021-01-01 21:52

    I think what's happening with this code :

    SQLQuery query = session.createSQLQuery("myNamedNativeQuery");
    query.setParameter(nameParameter1, value1);
    query.setParameter(nameParameter2, value2);
    query.uniqueResult(); 
    

    is this:

    at line 1 : a query plan is created based on some expected values for your named parameters.

    at line 4 : the query is executed with value1 and value2, but those values are not "good values" for the query plan that was elaborate at line 1 and so, the database is executing a very inappropriate plan for the actual values and it takes a lot of time.

    Why ?

    Looking at the source code of HibernateSessionImpl.createSQLQuery(...) I found this line of code:

    SQLQueryImpl query = new SQLQueryImpl(
                    sql,
                            this,
                            factory.getQueryPlanCache().getSQLParameterMetadata( sql )
            );
    

    which is calling getQueryPlanCache() with some parameterMetaData. I assume that this metadata is not good enough.

    0 讨论(0)
提交回复
热议问题