In .NET 4 and multicore environment, does the linq to sql datacontext object take advantage of the new parallels if we use DataLoadOptions.LoadWith?
EDIT
I know linq to sql does not parallelize ordinary queries. What I want to know is when we specify DataLoadOption.LoadWith, does it use parallelization to perform the match between each entity and its sub entities?
Example:
using(MyDataContext context = new MyDataContext())
{
DataLaodOptions options =new DataLoadOptions();
options.LoadWith<Product>(p=>p.Category);
return this.DataContext.Products.Where(p=>p.SomeCondition);
}
generates the following sql:
Select Id,Name from Categories
Select Id,Name, CategoryId from Products where p.SomeCondition
when all the products are created, will we have a
categories.ToArray();
Parallel.Foreach(products, p =>
{
p.Category == categories.FirstOrDefault(c => c.Id == p.CategoryId);
});
or
categories.ToArray();
foreach(Product product in products)
{
product.Category = categories.FirstOrDefault(c => c.Id == product.CategoryId);
}
?
No, LINQ to SQL does not. There is little to parallelize on the .NET side. All LINQ to SQL does is translating expression trees to SQL queries. SQL Server will execute those SQL statements, and is able to do this in parallel.
Don’t forget that while you can do something like this with your LINQ to SQL LINQ query, it isn’t a good idea:
While this yields the correct results, you won’t get the performance improvement you are hoping for. PLINQ isn’t able to handle
IQueryableobjects. Therefore, it will just handle anIQueryableas anIEnumerable(thus iterating it). It will process thedb.Customerscollection in parallel and use multiple threads to filter the customers. While this sounds okay, this means it will retrieve the complete collection of customers from the database! Without theAsParallelconstruct, LINQ to SQL would be able to optimize this query by adding theWHERE id = @IDto the SQL. SQL Server would than be able to use indexes (and possibly multiple-threads) to fetch the values.While there is some room for optimization inside the LINQ to SQL engine when it comes to matching entities to its sub entities, there doesn’t seem such optimization in the framework currently (or at least, I wasn’t able to find any using Reflector).