sql server query running slow from java

前端 未结 13 1922
渐次进展
渐次进展 2020-12-05 12:21

I have a java program that runs a bunch of queries against an sql server database. The first of these, which queries against a view returns about 750k records. I can run t

相关标签:
13条回答
  • 2020-12-05 13:03

    I know this is a very old question but since it's one of the first results when searching for this issue I thought that I should post what worked for me.

    I had a query that took about 3 seconds when I used SQL Server Management Studio (SSMS) but took 3.5 minutes when running using jTDS JDBC driver via the executeQuery method.

    None of the suggestion mentioned above worked for me mainly because I was using just Statement and not Prepared Statement. The only thing that worked for me was to specify the name of the initial or default database in the connection string, to which the connecting user has at least the db_datareader database role membership. Having only the public role is not sufficient.

    Here’s the sample connection string:

    jdbc:jtds:sqlserver://YourSqlServer.name:1433/DefaultDbName
    

    Please ensure that you have the ending /DefaultDbName specified in the connection string. Here DefaultDbName is the name of the database to which the user ID specified for making the JDBC connection has at least the db_datareader database role. If omitted, SQL Server defaults to using the master database. If the user ID used to make the JDBC connection only has the public role in the master database, the query takes exceptionally long.

    I don’t know why this happens. However, I know a different query plan is used in such circumstances. I confirmed this using the SQL Profiler tool.

    Environment details:

    • SQL Server version: 2016
    • jTDS driver version: 1.3.1
    • Java version: 11
    0 讨论(0)
提交回复
热议问题