views:

235

answers:

2

I'm having performance issues with Linq2Sql compared to raw ADO.NET which has led me down the path of compiled queries. I have got this far so far

public static readonly Func<MyDataContext, WebServices.Search.Parameters, IQueryable<image>>
    Compiled_SelectImagesLinq = 
        CompiledQuery.Compile<MyDataContext, WebServices.Search.Parameters, IQueryable<image>>(
            (dc, parameters) =>  from i in dc.images
                  join l in dc.links on i.image_id equals l.image_id
                  join r in dc.resolutions on i.image_id equals r.image_id
                  where i.image_enabled == true && i.image_rating >= parameters.MinRating
                  && i.image_rating <= parameters.MaxRating
                  select i
      );

However I can't figure out how to add the extra optional parameters to the query as I currently do

if (parameters.Country != null)
{
    query = query.Where(x => x.image_country_id == parameters.Country);
}

if (parameters.ComponentId != null)
{
    query = query.Where(x => x.links.Any(l => l.link_component_id == parameters.ComponentId));
}

etc, etc

I tried writing another function which does

var query = Compiled_SelectImagesLinq(parameters);

and then adding the extra parameters to the query and returning

return query.Distinct().Take(parameters.Results);

Bit this doesn't seem right and returns no results

A: 

Have a look at this article. It may not do what you need (especially since you are compiling your queries), but anytime someone mentions Dynamic and Linq in the same sentence, I refer them to this article:

Dynamic LINQ: Using the LINQ Dynamic Query Library

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Robert Harvey
A: 

You'd have to benchmark your specific query, but often queries must be used 10-20 times before compiled query performance improvements equal the overhead. Also, how are you adding parameters to the where clause?

Additionally, dynamic compiled queries seems a bit of a mismatch. The Dynamic LINQ query library will do what you need but I don't think you'll get the compiled query performance improvement you want.

marr75
The query will be executed 1000s of times a day. The parameters are generated as a result of a call to a web service from web pages to retrieve relevant images from cloud storage based on multiple criteria. Hope that makes sense. I'm starting to think I'm trying to force the Linq issue and should just use Raw ADO for performance and simpler free-text index searching
Nick Allen - Tungle139
But how many times will it be recompiled a day? Are you going to hold the compiled query in memory somewhere? Also, each dynamic query will be run 1000s of times a day? Additionally, I'm curious how query performance could make a difference when you have to download images anyway (isn't the transfer time much longer than the query time?). I'm not trying to criticize your design, I'm actually just intrigued by the idea of a situation that requires these characteristics.
marr75