我有一个执行以下操作的查询:
有什么技巧可以加快执行时间吗?
var authorizeType = new List<TreeNodeType> {
TreeNodeType.Equipment,
TreeNodeType.StructuredArea
};
var childrens = await (from n in _db.TreeNodes.Where(n => n.TreeNodeId == treeNodeParentId)
from path in _db.TreeNodes.Where(p => (EF.Functions.Like(p.Path, n.Path + "%")) && authorizeType.Contains(p.Type))
select new
{
path.Name,
path.FullName,
path.Type,
path.HorusId,
}).ToListAsync();
var childrensWithoutItems = childrens.Where(i => !_db.AvailableItems.Select(d => d.HorusId).Contains(i.HorusId));
var availableitems = await _db.AvailableItems.Where(x => x.HorusId == null || childrens.Select(d => d.HorusId).Contains(x.HorusId.Value)).ToListAsync();
//without items
var withoutItems =
(from c in childrensWithoutItems
from co in availableitems
join tree in childrens on co.HorusId equals tree.HorusId into left
from le in left.DefaultIfEmpty()
join i in items on co.ItemIdentifier equals i.Identifier
where le == null
select new OrganizationItemDetail {
OrganizationPath = c.FullName,
OrganizationChildName = c.Name,
OrganizationType = c.Type,
Name = i.Name,
Packaging = i.Packaging.ToString(),
ShortName = i.ShortName,
Type = i.Type.ToString(),
UnitValue = i.UnitValue,
});
//with items
var withItems = (from co in availableitems
join tree in childrens on co.HorusId equals tree.HorusId
join i in items on co.ItemIdentifier equals i.Identifier
select new OrganizationItemDetail {
OrganizationPath = tree.FullName,
OrganizationChildName = tree.Name,
OrganizationType = tree.Type,
Name = i.Name,
Packaging = i.Packaging.ToString(),
ShortName = i.ShortName,
Type = i.Type.ToString(),
UnitValue = i.UnitValue,
});
var un = withoutItems.Concat(withItems);
尝试添加 .AsNoTracking() 作为开始。