Consider the situation where I need to perform and combine the results of several isolated queries from the database. Each individual query is based on a parameter, which is supplied from elsewhere.
One way would be to perform these queries sequentially and union the results:
public IEnumerable<SomeEntity> GetSomeEntities(List<int> parameters)
{
var entities = new List<SomeEntity>();
using(var context = new MyContext())
{
entities = parameters.SelectMany(p=> SomeComplexQuery(p);
}
return entities;
}
The issue with the above solution is that performance is now proportional to the size of the parameter list. SomeComplexQuery is relatively resource-intensive.
Another solution would be to use parallelization:
public IEnumerable<SomeEntity> GetSomeEntities(List<int> parameters)
{
var entities = new List<SomeEntity>();
Parallel.ForEach(parameters, p =>
{
using(var context = new MyContext())
{
entities.AddRange(SomeComplexQuery(p)); // Assume thread-safety
}
};
return entities;
}
When I run the above solution, I get much better results, but I'm concerned:
What issues could arise from opening multiple contexts in parallel? What if we had a parameter list of size 20 or even more? Could system load result in further undesirable results?
Specifically for SQL Azure, would this be considered a bad idea due to the added latency when establishing new database connections?
I am using Entity Framework 6, Azure Web Roles and SQL Azure.