I am executing a stored procedure with one parameter in a foreach loop. I get the result and do some extra processing after that. Here's the code:
foreach(var id in GetIds())
{
var result = ExecuteStoreProcedureForResult(id);
//do extra processing with the result
}
When I execute the sp in directly from Query Analyzer the first time it takes 4-5 seconds. Every time after that the query the procedure returns in milliseconds, even with different ID parameters — much faster than the first time.
My question here is if I execute this sp from code, will the query plan be cached and return results faster after the first execution., in same way it did with Query Analyzer?
I am opening the connection once, executing the sp for each parameter, and finally closing the connection. Will that make a difference?
All suggestions appreciated.