views:

78

answers:

4

I'm using LINQ to SQL to access the database (SQL Server 2005). The first call takes up to 10 seconds to retrieve the data, a second call takes less than a second.

What can be done to improve the performance of the first call to the database? The database action happens in the controller of a asp.net mvc application.

Thanks

A: 

Not sure this kind of timeout is LINQ or ASP.NET related. Do you also notice the timeout when using the database with ADO.NET?

Fabian Vilers
It's guessing, but I guess as well that it's the ASP compilation which causes the delay at first.
Onots
Sorry, I'm quite new in this topic. Is it easy to switch to ADO.NET to compare the performance?
Martin
Yes, just write a small console program and check for sample code in MSDN. Have a look at SqlConnection, SqlCommand, SqlReader classes.
Fabian Vilers
+1  A: 

I believe what you are experiencing is SQL Server caching the query and is normal. Now if the original 10 seconds is too much, then you need to capture the sql query (I would suggest profiler) and then review it. In the past I would run the sql in the management console with show actual execution plan selected. There are resources on the web to explain how to read it, but it should help you to find the bottleneck. HTH

Edit I mean to say it is normal for long running queries to speed up after they have been run once, since SQL Server caches the query (I believe the execution plan to be exact) for later use.

Wade

Wade73
Thanks, the query is quite simple, so I guess it's a SQL server thing. I've read about the "View Generation" that takes more than the half of the query time. It's possible to pre-generate the view with EdmGen.exe, but that's not possible in L2S...
Martin
A: 

I doubt very much Linq-to-SQL is the culprit here. Can you post the T-SQL L2S is generating, along with rows counts and information on indexing?

Randy Minder
I'm sorry, but I don't have sufficient user rights on the db to run the sql server profiler. But as I've written above, the query isn't complex at all. The strange thing is that a second call on the same tables is much faster...
Martin
The second call is faster because of caching (as pointed out by Wade73). I agree with many of the other posters, if 10 seconds is too slow, the fix is going to indexing the tables you are querying. Keep in mind that simple queries can take a long time too; processing time is a function of the number of rows in the table, the number of rows returned, the processing required on the retunred rows (sorts, grouping, etc.) and the indexes available for retrieving the rows.
Jeff Siver
Thank you Jeff, I'll try indexing the tables and will come back later.
Martin
A: 

I think what you're experiencing is the Asp.Net compilation process the first time the page is loaded, not a performance problem with LTS. One way to measure performance is to profile it with the Linq to Sql Profiler. It will tell you exactly what the query is that is being generated as well as execution times for both the query and your code.

Chris Conway