Java SpringJDBCTemplate+Postgres,同样的请求非常慢
我有此SQL请求:Java SpringJDBCTemplate+Postgres,同样的请求非常慢,java,spring,postgresql,jdbc,Java,Spring,Postgresql,Jdbc,我有此SQL请求: SELECT num.top, num.top / 1.0E7 as value, num.grille_id, 13 as resultat_id FROM ( SELECT SUM(c.top + a.top2 * 900) as top, a.grille_id FROM exe_resultat a INNER JOIN exe_resultat_2 c ON c.resultat_id=11 AND c.grille_id = a.grille_id WHERE
SELECT num.top, num.top / 1.0E7 as value, num.grille_id, 13 as resultat_id FROM (
SELECT SUM(c.top + a.top2 * 900) as top, a.grille_id FROM exe_resultat a
INNER JOIN exe_resultat_2 c ON c.resultat_id=11 AND c.grille_id = a.grille_id
WHERE a.resultat_id =13 GROUP BY a.grille_id
UNION ALL
SELECT a.top2 * 900 as top, a.grille_id FROM exe_resultat a
LEFT JOIN exe_resultat_2 c ON c. resultat_id=11 AND c.grille_id = a.grille_id
WHERE a.resultat_id =13 AND c.grille_id IS NULL
UNION ALL
SELECT c.top, c.grille_id FROM exe_resultat_2 c
LEFT JOIN exe_resultat a ON a. resultat_id=13 AND a.grille_id = c.grille_id
WHERE c.resultat_id =11 AND a.grille_id IS NULL) num ORDER BY num.grille_id;
在PGAdmin上,它在不到1秒的时间内返回大约15000行。
当我使用SpringJDBC在Java应用程序中执行它时,有时所需时间也不到1秒,但有时所需时间远远超过1分钟。
请求挂起超过1分钟的可能性约为50%。
发生这种情况时,我可以在PGAdmin服务器状态选项卡上看到请求仍在执行
它没有被其他事务阻止。我试图增加JDBC的大小。我试图用LIMIT和OFFSET对请求进行分页,每次只返回1000行。不管有没有准备好的陈述,我都试过了。
我还尝试在JUnit测试中执行请求100次,没有问题
它只发生在应用程序上,该应用程序是一个批处理应用程序,其中多个线程执行器按特定顺序在数据库中执行操作。我怀疑这个多线程环境与问题的原因有关 我终于解决了这个问题。第一次和第二次请求的计划不一样。
执行真空分析可以解决问题。所以这是一个纯粹的PostgreSQL问题。java代码对结果做了什么?您确定这不是处理结果的问题吗?您可以使用探查器运行应用程序,当出现此问题时,请检查问题是否确实在数据库调用中,而不是在处理过程中。