AndyV
AndyV

Reputation: 398

Entity Framework Performance Inconsistency Compared to Sql Management Studio

I'm getting timeouts with a very basic EF statement. I'm simply doing a select from a single table with a Entity.Title.StartsWith("test") and a .Take(25). When I run this for a search that returns no results I get a timeout.

If I profile and grab the sql statement it looks fine, and if I run that sql in Management Studio it runs in a fraction of a second!

Why would the same query run sub-second in Management Studio and timeout when generated by EF and called from an Asp.Net app?

Upvotes: 2

Views: 763

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300827

It probably means you need to rebuild statistics.

This is a common symptom of an incorrectly cached query plan due to out of date statistics.

See this answer: Why are there performance differences when a SQL function is called from .Net app vs when the same call is made in Management Studio

This will update all statistics and refresh views and stored procs (but be careful about running on a Production machine):

EXEC sp_updatestats 

EXEC sp_refreshview  

-- Probably won't need this as your are not using stored procs 
EXEC sp_msForEachTable 'EXEC sp_recompile ''?''' 

Upvotes: 6

Related Questions