0

I have at small web-application that get some data from a SQLdb in Visual Studio 2010. When i try to display this, using a simple dropdownlist it takes around 15 sec the first time just after compilation and then after that 5 sec every request. I use a LINQ connection to db and a repeater to print everything out. It´s four columns and around 50 rows, so not so mutch data. It doesn´t matter if i try take 10 rows, same response time.

We have tested the same thing on another computer with VS2008 installed and the time was like 0.1 sec or something like that. I use .NET 3.5 and have installed all the latest updates (SP1 and so).

If i look at the trace i see it takes time at these rows in my code:

var cust = dbContext.Customers
    .Select(c => new
    {
        c.customerID,
        c.Email
    }).Take(10);

repeater.DataSource = cust;
repeater.DataBind();

Anybody know what could be taking som mutch time?

Regards Daniel

I have tried this other aproach:

SqlConnection connection = new SqlConnection(connectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connection;
        cmd.CommandType = CommandType.Text; //default

        cmd.CommandText = "SELECT Email FROM Customer";

        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();

It takes just as long.. there must be some other more fundamental problem than the code to call the db i think?

Some more information: I did som trace, this is first time page is loaded:

Begin Raise ChangedEvents   0,00219604937555932 0,000014
After repeater has printed  15,8138335259613    15,811637
End Raise ChangedEvents 15,8138801733289    0,000047

Second time:

Begin Raise ChangedEvents   0,00219604937555932 0,000014
After repeater has printed  5,25090396258066    5,248825
End Raise ChangedEvents 25095106283536          0,000047

What´s happening at ChangeEvents?

daniel_aren
  • 1,714
  • 3
  • 24
  • 41
  • 1
    Did you try calling ToList() after Take() ? – hyp Feb 24 '12 at 13:47
  • 4
    Did you check the database server to see how long the query is taking? – mbeckish Feb 24 '12 at 13:47
  • As an aside, I would name `cust` something more meaningful like `customerEmailQuery` and move the `.Take(10);` to the data binding line. It's important to make a clear distinction between variables that represent a linq query, and variables that are the result of the query. – asawyer Feb 24 '12 at 13:48
  • It could also be the initial asp.net start up - are you hosting the web site in IIS on your VS2010 machine? See http://stackoverflow.com/questions/2221292/asp-net-application-runs-slow-at-first-time for example. – dash Feb 24 '12 at 14:01
  • @dash: i am hosting on my VS2010 machine.. – daniel_aren Feb 24 '12 at 15:37

2 Answers2

1

Get the actual SQL being generated by LINQ, copy-paste it to your database management tool and look at the execution plan to identify potential performance issues.

You can see the actual SQL by using Log property of the data context. In a console application, you can do this:

dbContext.Log = Console.Out;
// ...

Or, write the SQL to file like this:

using (var writer = new StreamWriter("sql.log")) {
    dbContext.Log = writer;
    // ....
}

Also, it might be worth adding ToList() or ToArray() at the end of your LINQ expression, just to make sure it is not unintentionally re-executed.

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
0

I have no idee what i did to make it work.. just kept on doing stuff at other places in the code and updated latest windows update.. hmm.. now it work like a charm, must be a bug in VS2010?? Thanks for all suggestions though, learned some great stuff!

Regards

daniel_aren
  • 1,714
  • 3
  • 24
  • 41