简体   繁体   English

使用Hibernate调用MySQL存储过程时的性能问题

[英]Performance issues when calling MySQL stored procedure using Hibernate

I'm trying to understand why the execution time of my stored procedure is so much higher when I run it from Java using Hibernate than when I run it directly in MySQL. 我试图理解为什么我使用Hibernate从Java运行存储过程的执行时间比直接在MySQL中运行的存储时间长得多。

The stored procedure itself is responsible for moving 20000 rows from table A to table B and then delete them in table A. 存储过程本身负责将20000行从表A移到表B,然后在表A中将其删除。

Running the stored procedure in MySQL takes around 18 seconds. 在MySQL中运行存储过程大约需要18秒钟。

In Java, I'm using Hibernate and create a query: 在Java中,我正在使用Hibernate并创建一个查询:

Query query =
    mainSession
        .createSQLQuery("{CALL my_stored_procedure(:maxResultSize)}")
        .setParameter("maxResultSize", maxResultSize);

Then the query is executed and the session is flushed and cleared: 然后执行查询,并刷新并清除会话:

List<BigInteger> rows = query.list();
mainSession.flush();
mainSession.clear();

This takes around 248 seconds. 这大约需要248秒。

Does anyone know why it takes so much more time to call the stored procedure from Java using Hibernate? 有谁知道为什么使用Hibernate从Java调用存储过程要花这么多时间?

What approach should I take to increase the performance? 我应该采取什么方法来提高性能?

Please could you try with native query, it is faster for me and work well. 请您尝试使用本机查询,它对我来说更快,并且运行良好。

List<Object[]> query = (List<Object[]>) mySessionFactory.getCurrentSession()
                .createNativeQuery("{CALL my_stored_procedure(:maxResultSize)}")
        .setParameter("maxResultSize", maxResultSize).getResultList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM