gpt4 book ai didi

java - sql server查询从java运行缓慢

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:03:29 25 4
gpt4 key购买 nike

我有一个 java 程序,它对 sql server 数据库运行一堆查询。其中第一个针对 View 的查询返回大约 750k 条记录。我可以通过 sql server management studio 运行查询,并在大约 30 秒内得到结果。但是,我昨晚启动了要运行的程序。今天早上我检查它时,大约 15 小时后,这个查询仍然没有将结果返回给 java 程序。

我可以访问数据库以执行任何我想做的事情,但我真的不确定如何开始调试它。应该怎么做才能弄清楚是什么导致了这种情况?我不是 dba,也不熟悉 sql server 工具集,因此如果您能提供更多有关如何执行您可能建议的操作的详细信息,我们将不胜感激。

代码如下

stmt = connection.createStatement();
clientFeedRS = stmt.executeQuery(StringBuffer.toString());

编辑1:

好吧,已经有一段时间了,这被搁置了,但这个问题又回来了。我研究了从 jdbc 驱动程序 v 1.2 升级到 2.0,但我们停留在 jdk 1.4 上,而 v 2.0 需要 jdk 1.5,所以这是一个非启动器。现在我正在查看我的连接字符串属性。我看到 2 可能有用。

SelectMethod=cursor|direct
responseBuffering=adaptive|full

目前,由于存在延迟问题,我使用游标作为 selectMethod 运行,并使用已满的 responseBuffering 默认值。改变这些属性可能有帮助吗?如果是这样,理想的设置是什么?根据我在网上可以找到的内容,我在想,使用直接选择方法和自适应响应缓冲可能会解决我的问题。有什么想法吗?

编辑2:

好吧,我结束了更改这两个连接字符串参数,使用默认选择方法(直接)并将 responseBuffering 指定为自适应。这最终对我来说效果最好,并减轻了我所看到的延迟问题。感谢所有的帮助。

最佳答案

我遇到了类似的问题,在 Java 中使用 jdbc 连接时,一个非常简单的请求 (SELECT . FROM . WHERE = .) 最多需要 10 秒才能返回一行,而在 sqlshell 中只需要 0.01 秒。无论我使用的是官方 MS SQL 驱动程序还是 JTDS 驱动程序,问题都是一样的。

解决方案是在 jdbc url 中设置此属性:sendStringParametersAsUnicode=false

完整示例,如果您使用的是 MS SQL 官方驱动程序:jdbc:sqlserver://yourserver;instanceName=yourInstance;databaseName=yourDBName;sendStringParametersAsUnicode=false;

如果使用不同的 jdbc 驱动程序和有关问题的更多详细信息,请参见此处的说明:http://emransharif.blogspot.fr/2011/07/performance-issues-with-jdbc-drivers.html

SQL Server differentiates its data types that support Unicode from the ones that just support ASCII. For example, the character data types that support Unicode are nchar, nvarchar, longnvarchar where as their ASCII counter parts are char, varchar and longvarchar respectively. By default, all Microsoft’s JDBC drivers send the strings in Unicode format to the SQL Server, irrespective of whether the datatype of the corresponding column defined in the SQL Server supports Unicode or not. In the case where the data types of the columns support Unicode, everything is smooth. But, in cases where the data types of the columns do not support Unicode, serious performance issues arise especially during data fetches. SQL Server tries to convert non-unicode datatypes in the table to unicode datatypes before doing the comparison. Moreover, if an index exists on the non-unicode column, it will be ignored. This would ultimately lead to a whole table scan during data fetch, thereby slowing down the search queries drastically.

在我的例子中,我在搜索的表中有超过 3000 万条记录。应用该属性后,完成请求的持续时间从 10 多秒减少到大约 0.01 秒。

希望这会对某人有所帮助!

关于java - sql server查询从java运行缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/961078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com