Spring JdbcTemplate在调用SQL Server存储过程时非常缓慢。

huangapple go评论73阅读模式
英文:

Spring JdbcTemplate is extremely slow when call SQLserver Stored procedure

问题

我正在使用JdbcTemplate.execute() 调用与SQLServer数据库连接的存储过程。 使用相同的参数,当我在SQL Server Management Studio中直接执行该存储过程时,它会在1秒内完成,但是当我在Java中使用JdbcTemplate时,它会执行几分钟,导致严重的性能问题。

请注意,该存储过程包含 "SET ANSI_NULLS ON" 和 "SET QUOTED_IDENTIFIER ON"。

英文:

I'm using JdbcTemplate.execute() to call a SP connected with SQLServer database.
With the same parameters, when I execute the SP directly in SQL Server Management Studio it finishes within 1s, but when I use JdbcTemplate in java it execute over minutes with causes severe performance issue.

Note the stored procedure contains "SET ANSI_NULLS ON" and "SET QUOTED_IDENTIFIER ON"

答案1

得分: 0

经过一些测试,我终于发现问题出在我存储过程的空值判断上:

我为存储过程设置了一些默认参数,并将它们设为 @A nvarchar(255)='',然后在逻辑中,我需要将它们用作逻辑条件:if(@A is not null and rtrim(ltrim(@A)) <>'')。
就是这么简单,在管理工作室中运行得非常好。

将初始值更改为 @A nvarchar(255) 并且使用 if(@A is not null) 后,JdbcTemplate 也会返回结果……所以对于 JdbcTemplate,肯定有一些内部逻辑来进行空值检查,但我不太确定是什么以及如何实现的。

英文:

After some testing I finally find that the issue lies on blank values judgement in my SP:

I have some default parameters for the SP and set them as @A nvarchar(255)='', then during the logic I need to use them as logic condition: if(@A is not null and rtrim(ltrim(@A)) <>'')
Just that simple and it works totally good in the Management Studio.

After change that init to @A nvarchar(255) and use if(@A is not null), it also returns the results in JdbcTemplate... so there should be some inner logic for JdbcTemplate to do that empty check but I'm not sure why and how.

huangapple
  • 本文由 发表于 2020年8月31日 15:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63666782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定