我有一个大约有5,000行的数据库。还有许多多对多的关系。作为“高级搜索”查询的一部分,我需要跨表格进行自由文本搜索。
我创建了一个强类型数据集,并在应用启动时从SQL Server导入所有数据。在对数据集执行LINQ查询时,查询执行速度非常慢(大约15秒)。我认为针对内存数据集执行查询会比SQL Server快得多,但似乎并非如此。我甚至需要在where子句中添加更多连接和“搜索”,所以事情只会变得更糟。
在我正在搜索的字段中,最长的是Summary,而数据库中最长的是小于2,000字节,所以我们不是在讨论要搜索的大量数据。我在这里咆哮错误的树,还是有办法改善这个查询的性能?
以下是代码:
var results = from e in _data.ds.Employee
join es in _data.ds.EmployeeSkill on e.EmployeeId equals es.EmployeeId into esGroup from esItem in esGroup.DefaultIfEmpty()
join s in _data.ds.Skill on esItem?.SkillId equals s.SkillId into sGroup from skillItem in sGroup.DefaultIfEmpty()
join er in _data.ds.EmployeeRole on e.EmployeeId equals er.EmployeeId into erGroup from erItem in erGroup.DefaultIfEmpty()
join r in _data.ds.Role on erItem?.RoleId equals r.RoleId into rGroup from rItem in rGroup.DefaultIfEmpty()
join et in _data.ds.EmployeeTechnology on e.EmployeeId equals et.EmployeeId into etGroup from etItem in etGroup.DefaultIfEmpty()
join t in _data.ds.Technology on etItem?.TechnologyId equals t.TechnologyId into tGroup from tItem in etGroup.DefaultIfEmpty()
where
e.FirstName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
e.LastName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
e.RMMarket.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
!e.IsSummaryNull() && e.Summary.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0
select new SearchResult
{
EmployeeId = e.EmployeeId,
Name = e.FirstName + " " + e.LastName,
Title = e.Title,
ImageUrl = e.IsImageUrlNull() ? string.Empty : e.ImageUrl,
Market = e.RMMarket,
Group = e.Group,
Summary = e.IsSummaryNull() ? string.Empty : e.Summary.Substring(1, e.Summary.Length < summaryLength ? e.Summary.Length - 1 : summaryLength),
AdUserName = e.AdUserName
};
答案 0 :(得分:1)
一些想法:
首先,您正在搜索字符串。如果要搜索很多内容,请考虑维护全文索引以加快速度。
其次,将where
子句放在join
子句之前。过滤掉数据的东西应尽可能高在LINQ语句中。它目前正在为每一行加入一堆数据,即使在where
子句为假的情况下也不会使用它。
答案 1 :(得分:1)
假设您仍然加载到DataSet
而不是对象列表(没有足够的信息来翻译该部分),我建议这样做:
预先加入要用作搜索索引的数据:
var searchBase = (from e in _data.ds.Employee
join es in _data.ds.EmployeeSkill on e.EmployeeId equals es.EmployeeId into esGroup
from esItem in esGroup.DefaultIfEmpty()
join s in _data.ds.Skill on esItem?.SkillId equals s.SkillId into sGroup
from skillItem in sGroup.DefaultIfEmpty()
join er in _data.ds.EmployeeRole on e.EmployeeId equals er.EmployeeId into erGroup
from erItem in erGroup.DefaultIfEmpty()
join r in _data.ds.Role on erItem?.RoleId equals r.RoleId into rGroup
from rItem in rGroup.DefaultIfEmpty()
join et in _data.ds.EmployeeTechnology on e.EmployeeId equals et.EmployeeId into etGroup
from etItem in etGroup.DefaultIfEmpty()
join t in _data.ds.Technology on etItem?.TechnologyId equals t.TechnologyId into tGroup
from tItem in etGroup.DefaultIfEmpty()
select new {
e.FirstName, e.LastName, e.RMMarket, e.Summary,
e.EmployeeID, e.Title, e.ImageUrl, e.Group, e.AdUserName
}).ToList();
针对加载和加入的数据运行搜索:
var results = from e in searchBase
where
e.FirstName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
e.LastName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
e.RMMarket.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
!e.IsSummaryNull() && e.Summary.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0
select new SearchResult {
EmployeeId = e.EmployeeId,
Name = e.FirstName + " " + e.LastName,
Title = e.Title,
ImageUrl = e.IsImageUrlNull() ? string.Empty : e.ImageUrl,
Market = e.RMMarket,
Group = e.Group,
Summary = e.IsSummaryNull() ? string.Empty : e.Summary.Substring(1, e.Summary.Length < summaryLength ? e.Summary.Length - 1 : summaryLength),
AdUserName = e.AdUserName
};
顺便说一句,你的示例代码没有显示连接的原因,因为没有任何连接范围变量在条件或答案中使用,并且你仍然无论如何都要加入每个连接,所以将它们排除在外是最快的解决方案。