2009-06-19 45 views
0

與原始ADO.NET相比,我遇到了Linq2Sql的性能問題,這導致了我編譯查詢的路徑。我有這麼遠到目前爲止如何編寫動態Linq2Sql編譯查詢?

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

但我無法弄清楚如何將額外的可選參數添加到查詢作爲我目前做

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

等,等

我嘗試編寫另一個功能

var query = Compiled_SelectImagesLinq(parameters); 

然後將其他參數添加到查詢中並返回

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

位這似乎並不正確,並沒有返回結果

回答

0

你不得不基準您的具體問題,但往往查詢必須在使用之前編譯的查詢性能的改進10-20倍等於開銷。另外,你如何將參數添加到where子句?

此外,動態編譯查詢似乎有點不匹配。動態LINQ查詢庫將執行您所需的操作,但我認爲您不會獲得所需的編譯查詢性能改進。

+2

查詢將每天執行1000次。這些參數是作爲從網頁調用Web服務的結果生成的,以基於多個標準從雲存儲中檢索相關圖像。希望這是有道理的。我開始認爲我試圖強制Linq問題,應該只使用Raw ADO來提高性能和簡化自由文本索引搜索 – 2009-06-19 15:57:59