views:

78

answers:

4

I am using Entity Framework to layer on my SQL Server 2008 database. The EF is present in my web service and the webservice is invoked by a Silverlight client.

I am seeing a serious performance issue in terms of the duration taken by a query to execute in the EF. This wouldn't happen in the consecutive calls.

A little bit of googling revealed that, it's caused per app domain to construct the in-memory model of the db objects. I found this Microsoft link explaining pre-generation of views for performance improvement. Even after implementing the steps, the performance actually degraded instead of improving. I am curious, if anyone has tried this approach successfully and if there are any other avenues for improving performance.

I am using .NET 3.5.

+2  A: 

The same laws of physics apply for EF queries as they do for ordinary SQL. Check your database tables and make sure that you have indexes on primary and foreign keys, that your database is properly normalized, and so forth. If performance is degrading after Microsoft's suggestions, then that's my guess as to the problem area.

Robert Harvey
A: 

Use SQL Profiler to check how many queries executed to retrieve your data.If it's large number use Include() method of ObjectQuery to retrieve child objects with parent in one query.

bniwredyc
+1  A: 

A couple areas to look at for EF performance

  1. Do as much of the processing before calling things like tolist(). ToList will bring everything in the set into memory. By default, EF will keep building the expression tree and only actually process it when you need the data in memory. That first query will be against the database, but afterwards the processing will be in memory. When working with large data, you definitely want as much of the heavy lifting done by the database as possible.
  2. EF 1 only has the option to pull the entire row back. Therefore if you have a column that is a large string or binary blob, it is going to be pulled down and into memory whether you need it or not. You can create a projection that doesn't include this column, but then you don't get the benefits of having it be an entity.
  3. You can look at the sql generated by EF using the suggestion in this post http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework
Jacob Adams
+1  A: 

Are you hosting the webservice in IIS? Is it running on the same site as the Silverlight App? What about the database itself? Is it running on a dedicated machine? Are there other apps hitting it? The first call to a dormant database is painful (I've had situations where it would actually time out in my environment.)

There are a number of factors to take into consideration here. But it comes down to more than just EF's overhead.

edit I didn't fully qualify but the process of opening the first connection to SQL Server is slow regardless of your data access solution.

Mike Brown
@Mike: I am currently running on a test bed IIS environment with SQL Server on same machine. It takes around 2 mins to get through the ToList method executed in the EF. What other pointers would you suggest me to look at?
pencilslate
I would say that it's a non-issue. One route however would be to use a self-hosted WCF service and have the service call a simple routine during startup so that it's already established communication with the SQL Server before you make a client call.The primary issue here is that you're testing on the same machine. If you see the problem on your production machines then it might be worth addressing. But there are ways to mitigate it.
Mike Brown