After upgrade to SQL 2016 Dynamic SQL Query Times out through .NET SQL Client but not Via SSMS

Dave L 1 Reputation point
2021-03-23T17:39:52.987+00:00

Our old production SQL server was 2008 Standard, we migrated databases to our "newer" SQL 2016 Standard instance. This instance has been running for several years. Many of our .NET applications use dynamic SQL queries to retrieve data, in this case they do not use any stored procedure only dynamic SQL.

We connect via the .NET SQL Client (System.Data.SqlClient.SqlConnection) where the queries get executed via SqlCommand. The default timeout is the standard 30 seconds, in this case well enough time to execute the query. Since moving to 2016 we always get query timeouts. Running the same query through SSMS, runs in 100ms without issue.

Changing the code to use .NET SQL ODBC (System.Data.Odbc.OdbcConnection) also works without issue in about 130ms.

I've also setup a couple test instances of SQL 2016 Express and 2019 Express and restored the same database to them, both test instances run the query in 130ms using both the .NET SQL Client and also the ODBC .NET Client (as well as manually in SSMS).

We've done the following, all have failed to correct the problem
ARTHIBORT = ON (to simulate SSMS's connection)
set the legacy cardinality estimation to on.
Rebuilt all table indexes
Rebuilt statistics
Reorganized indexes
Updated SQL 2016 to latest CU16
Set the timeout on the SQL Connection to 10 mins (normal query takes 100ms)
Clear the execution plan cache

We've considered the parameter sniffing possibilities or other query performances, but again this query runs in 100ms via SSMS or another SQL express instance. Yes there are "select *" statements (insert comments here) but again these queries are "light-weight" IMO as they run without notice via the methods listed above. The query has about 19 inner joins on it, by no means excessive from other examples.

Any tips would be greatly appreciated.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,437 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 116.5K Reputation points MVP
    2021-03-23T22:33:28.55+00:00

    I discuss this situation in my aritcle Slow in the Application, Fast in SSMS?. It seems that you have already covered some of the things I discuss, but some of the troubleshooting I discuss may help you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.