Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is some sql query much slower when used with SqlCommand?

I have a stored procedure that executes much faster from Sql Server Management Studio (2 seconds) than when run with System.Data.SqlClient.SqlCommand (times out after 2 minutes).

What could be the reason for this?


Details: In Sql Server Management Studio this runs in 2 seconds (on production database):

EXEC sp_Stat
    @DepartmentID = NULL

In .NET/C# the following times out after 2 minutes (on production database):

string selectCommand = @"
EXEC sp_Stat
    @DepartmentID = NULL";
string connectionString = "server=***;database=***;user id=***;pwd=***";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(selectCommand, connection))
    {
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
            }
        }
    }
}

I also tried with selectCommand = "sp_Stat", CommandType = StoredProcedure, and an SqlParameter, but it's the same result.

And without EXEC it's the same result as well.

On an almost data-empty development database both cases finishes in less than 1 second. So it's related to that there's a lot of data in the database, but it seems to only happen from .NET...


What Marc Gravell wrote about different SET values makes the difference in the presented case.

SQL Server Profiler showed that Sql Server Management Studio runs the following SET's that .NET Sql Client Data Provider does not:


SET ROWCOUNT 0 
SET TEXTSIZE 2147483647 
SET NOCOUNT OFF 
SET CONCAT_NULL_YIELDS_NULL ON 
SET ARITHABORT ON 
SET LOCK_TIMEOUT -1 
SET QUERY_GOVERNOR_COST_LIMIT 0 
SET DEADLOCK_PRIORITY NORMAL 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
SET ANSI_NULLS ON 
SET ANSI_NULL_DFLT_ON ON 
SET ANSI_PADDING ON 
SET ANSI_WARNINGS ON 
SET CURSOR_CLOSE_ON_COMMIT OFF 
SET IMPLICIT_TRANSACTIONS OFF 
SET QUOTED_IDENTIFIER ON
SET NOEXEC, PARSEONLY, FMTONLY OFF

When I included these, the same query took the same amount of time in SSMS and .NET. And the responsible SET is ...

SET ARITHABORT ON

What have I learnt? Maybe to use a profiler instead of guessing...

(The solution at first seemed to be related to parameter sniffing. But I had mixed some things up...)

like image 967
Ole Lynge Avatar asked Apr 29 '09 11:04

Ole Lynge


People also ask

What causes SQL queries to run slow?

Tasks are taking too long. Applications running slowly or timing out. Some queries taking forever.

How fix slow running query in SQL Server?

Step 1: Identity Blocking for Slow Queries The first step is to check for any blocking scenario. I will suggest you use the following SQL stored procedure and see if any session is blocking any other session. If there is any blocking, you can investigate those queries and find the reason for blocking and resolve it.


1 Answers

Another thing that can be important is the SET options that are enabled. Some of these options change the query plan sufficiently to change the profile. Some can have a huge impact if you are looking at (for example) a calculated + persisted (and possibly indexed) column: if the SET options aren't compatible, it can be forced to re-calculate the values, rather than using the indexed value - which can change an index seek into a table scan + calculation.

Try using the profiler to see what SET options are "in play", and see if using those options changes things.

Another impact is the connection string; for example, if you enable MARS that can change the behaviour in subtle ways.

Finally, transactions (implicit (TransactionScope) or explicit) can have a huge impact, depending on the isolation level.

like image 96
Marc Gravell Avatar answered Sep 29 '22 15:09

Marc Gravell