我正在开发一个应用程序,用户可以按类别过滤记录。
记录必须为1,但可以有多个类别。 (1 - *)
我的问题是,我该怎么做才能改善这种搜索?由于以下原因,它目前在大约O(n ^ 3)处运行:
交易可能有很多记录(1-many) 记录可能有很多类别(1-many) 迭代选择用于搜索的类别
我正在做的基本概要如下:
retrieve all announcements
if(startDate and endDate have values)
model = model.Where(x => x.WADate >= start.Value && x.WADate <= end.Value).ToList();
if(queryString is not null)
model = model.Where(
x => x.WANum.Contains(query) ||
x.Tooltip.ToLower(CultureInfo.InvariantCulture).Contains(query)
|| x.Topic.ToLower(CultureInfo.InvariantCulture).Contains(query)
).ToList();
if (selectedCategories.Count > 0)
{
bool HasMatch;
foreach (var ancmt in announcements)
{
HasMatch = false;
foreach (var cat in selectedCategories)
{
foreach (var xref in ancmt.waXref)
{
if (cat.ID == xref.WACategoryID)
{
HasMatch = true;
}
}
}
if(HasMatch)
{
model.Add(new EditViewModel
{
WATypeID = ancmt.WATypeID,
WANum = ancmt.WANum,
WATypeName = ancmt.waType.WATypeDescription,
Link = ancmt.Link,
Tooltip = ancmt.Tooltip,
Topic = ancmt.Topic,
WADate = ancmt.WADate,
WAID = ancmt.WAID,
});
ancmt.waXref.ToList().ForEach(
x => model.Last().Categories.Add(
new CategoryViewModel { ID = x.WACategoryID, Name = x.waCategory.WACategory, IsSelected = false }));
}
}
}
// If no catgories were selected, keep all announcements for next stage of search
else
{
foreach (var ancmt in announcements)
{
model.Add(new EditViewModel
{
WATypeID = ancmt.WATypeID,
WANum = ancmt.WANum,
WATypeName = ancmt.waType.WATypeDescription,
Link = ancmt.Link,
Tooltip = ancmt.Tooltip,
Topic = ancmt.Topic,
WADate = ancmt.WADate,
WAID = ancmt.WAID,
});
ancmt.waXref.ToList().ForEach(
x => model.Last().Categories.Add(
new CategoryViewModel { ID = x.WACategoryID, Name = x.waCategory.WACategory, IsSelected = false }));
}
}
我使用的是Method语法,而不是查询,更喜欢保留方法语法。
答案 0 :(得分:1)
对于任何真正的性能提升,最好创建特定的存储过程来处理数据库中的真正繁重处理,而不是客户端PC。