Query called running LINQ times out when same Query running in Management Studio completes in about 4 seconds.

I have a stored procedure in my MS SQL SERVER 2012 database that is called using LINQ (.NET 4.5) to populate a scheduler control.

The query completes in about 4 seconds normally both for the application calling it via LINQ and running the same query using management studio.

PROBLEM: Every few weeks the application starts timing out on that query when called using LINQ. Query runs fine via management studio.

I can fix the issue by essentially altering the SQL of the stored procedure and then altering it back to the original SQL. Then the query runs fine using LINQ again.

Any ideas what would cause this where a query gets executed differently from LINQ than it does using management studio?
LVL 2
YMartinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
You're probably falling victim to parameter sniffing.  Basically the system "guesses" at the best execution plan, and sometimes caches a bad one.
When you hit it from mgmt. studio, it seems to refresh the plan and run with that one.

http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi YMartin;

You state the following, "I have a stored procedure in my MS SQL SERVER 2012 database that is called using LINQ", and then you state this, "I can fix the issue by essentially altering the SQL of the stored procedure and then altering it back to the original SQL. ", Linq to SQL or Linq to Entity Framework does not build the query it only sends a command for the SQL server to execute a stored procedure which is defined in SQL server. All that Linq is doing is sending a command and the command is actually being sent by ADO.Net which Linq sits on top of and communicate with the server and is mature product.  Seeming that the issue gets corrected when making some changes on the server I would look at the server end.
0
Anthony PerkinsCommented:
Why don't you start by posting the query and all relevant schemas?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
YMartin, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.