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

前端 未结 3 1344
难免孤独
难免孤独 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: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.

提交回复
热议问题