views:

133

answers:

1

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);
}

?

+4  A: 

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 rome 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).

Steven
Thanks for your answer. I knew this for this specific case. What I would like to know it is what does happen when dataloadoptions are specified ?
Gregoire
I'm sorry. I'm not sure what you want to know. As far as I know, LINQ to SQL will do no internal acceleration by using parallel constructs. Of course you are free to do your own parallel optimization in the .NET space with PLINQ (to objects) when LINQ to SQL is done fetching.
Steven

related questions