views:

216

answers:

4

This question is an follow-up of an previous asked question.

I've written an simple program who looks like this.

string s;
do
{
  using (SqlConnection conn = new SqlConnection("POOLEDCONN"))
  {
    using (SqlCommand cmd = new SqlCommand("", conn))
    {
        s = (string) cmd.ExecuteScalar();
        Code2IncrementPerfomanceCounter
    }
  }    
} while (!string.IsNullOrEmpty(s))

The query returns an string(nvarchar(max), current maximum size 9k), there is a lot of latency between SQL Server and .NET. The SQL Profiler says that the query has an duration of 0 ms, so I think it isn't the database. The network is responding in less than 1 ms so it shouldn't be the network.

If I run this code on our testserver(VMWare, SQL isn't virtualized) there will be a max of 600 loops per second. The application doesn't consume more than 5 % cpu. Why isn't it going faster? Must I use streaming to get the data from SQL or something else?

Thanx in advance

A: 

How are you determining this latency?

Noon Silk
With performance counters
JSC
So, sorry, but what are they reporting? is it 600 per second that's slow? Obviously your posted code has no actual SQL; what is your SQL? How do you test that it runs in SQL fast? Is it the same DB in both environments?
Noon Silk
The SQLServer profiler continuously say that my stored proc is executed with an duration of 0ms. If I execute this application two times the 600 p/s grows to 1200 p/s. So SQL-server should not be the problem I think ;-)
JSC
Wait, I notice you've edited your post to consider 'code to increase perf counter'. What is that code?
Noon Silk
pc.Increment();
JSC
Amusing. So what I'm obviously suggesting is perhaps that is the cause of your speed issues.
Noon Silk
If I don't use my own performance counters. But the "SQL Statistics - Batch requests/sec" counter. It is still around 600 p/sec
JSC
And have you confirmed that this is the bottleneck? Can SQL actually process this query faster? I know the profiler claims it takes 0s, but I don't think that means it can actually do an infinite number of them per second.
Noon Silk
A: 

600 calls per second is one call per 1.7 ms. If your latency is 1 ms, and Sql Server adds a little to that, that seems like a plausible result?

Andomar
I understand but if I run the program locally on the sqlserver there is also a max of 600 calls per second
JSC
What makes you think 600 (or 1200) queries per second is so slow there has to be an explanation for it?
Andomar
It isn't slow but I see latency between sql-resultset(nvarchar(max)) and .net(string). Our SQL server isn't using alot of CPU(under 20%) and our app servers doesn't consume more than 5%(if we run it single threaded)
JSC
When you say "I see latency", what do you mean? I don't see any evidence that the "latency between sql-resultset(nvarchar(max)) and .net(string)" is the limiting factor in your application's performance.
Andomar
I think you are right. I've read somewhere on stackoverflow that the SQL Server Profiler rounds the duration to 0 ms if an result is in the range of 0-15 ms. So it could be the query.
JSC
A: 

I have three different things you need to look at:

  1. Is your web server connecting to the DB Server with the TCP/IP or are you using a different protocol?
  2. How busy is the network on the machine running the dtoNet process?
  3. Does the performance go above 600 processes/second if you remove the SQL call and leave only the performance counter logging?
Jeff Siver
1.) The SQL Server 2005 Network Configuration has Shared Memory -)2.) The network utilization is under a percent(Taskmanager 1Gbs)3.) I changed the query to return always the same record and then it is 1200 call/sec
JSC
Your connection string can override the default protocol so you should verify you are using TCP/IP for the remote connections. Alsom since changing the query to return a static result improved things, I'd say the problem is either the query itself or the amount of data returned by the query (if you are consistently over 4K of data, it will be split inot multiple packets which will slow things down a bit).
Jeff Siver
A: 

Opening and closing the SQL connection is expensive. Can you just do that once instead?

David
Connections are pooled, and opening and closing is free.
Andomar
.NET uses connectionpooling on an connectionstring, so this shouldn't be the problem.
JSC