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:
// BAD CODE!!! Don't parallelize a LINQ to SQL query
var q =
from customer in db.Customers.AsParallel()
where customer.Id == 5
select customer;
While this yields the correct results, you won't get the performance improvement you are hoping for. PLINQ isn't able to handle IQueryable
objects. Therefore, it will just handle an IQueryable
as an IEnumerable
(thus iterating it). It will process the db.Customers
collection 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 the AsParallel
construct, LINQ to SQL would be able to optimize this query by adding the WHERE id = @ID
to 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With