获得快速匹配所有项目的最佳方式

时间:2015-09-06 17:52:55

标签: c# sql model-view-controller

以下是相关代码片段。我们正在尝试将项目与项目属性的集合进行匹配。下面的代码有效,我们返回我们需要的东西,但它很慢。我们很好奇最好的方法是做什么。任何帮助是极大的赞赏。我们也对SQL中的选项持开放态度。谢谢!

var items = await GetItemsAsync(repository, a, b);
// Next we need to get all of the properties for those items. 
var results = new List<Result>();
foreach(var c in items)
{
   c.Properties = await GetItemProperties(repository, c.Id);
   var matchedProperties = selectedProperties.Where(si => c.Properties.Any(i => i.Id == si.Id));
   if(matchedProperties.Count() > 0)
   {
       results.Add(new Result(c)
       {
           MatchedProperties = matchedProperties,
           Relevance = (decimal)matchedProperties.Count() / (decimal)selectedProperties.Count()
       });
   }
}

1 个答案:

答案 0 :(得分:1)

我认为性能不佳的主要原因是您正在浏览项目的结果并在每个项目中执行新的SQL查询。所以你不能确定有多少sql查询命中。查询量为1 +项目结果。

如果你改成它怎么样:

for day,value in array.iteritems():
    if not value:
    continue
    print array

如果你这样做,你将只有两(2)个SQL查询。

sql可能像:

var items = await GetItemsAsync(repository, a, b);

var ids = items.Select(c => c.id);

var properties = await GetItemsProperties(repository, ids);

 // map items and properties after this without sql queries