简体   繁体   English

Linq查询性能很慢

[英]Performance of Linq query is very slow

I have 700 records that have different columns with type string and I want to select all records using this code:我有 700 条记录,它们有不同的列类型为字符串,我想使用此代码 select 所有记录:

public virtual object GetAll()
{
    var AllList = unitOfWork.Repository<STUser>().GetAll().ToList();

    return (from STUser in AllList
            select new
            {
                STUser.ID,
                FullName = STUser.HRPerson.LastName,
                STUser.USR,
                STUser.Active,
                STUser.TryCount,
                STUser.Description           
            }).OrderByDescending(i => i.ID).ToList();
}

However, unitl Fill "var AllList" is good but when Linq query execution is very slow.但是,unitl Fill "var AllList" 很好,但是当 Linq 查询执行速度很慢。 I removed FullName = STUser.HRPerson.LastName, line and it was executed very fast.我删除了FullName = STUser.HRPerson.LastName,行,它执行得非常快。 Join clause slowed it down. Join子句减慢了它的速度。

I ran SQL profiler for every row in the List and it looks like it executes the Query 700 times.我为列表中的每一行运行了 SQL 分析器,看起来它执行了 700 次查询。 Query is查询是

exec sp_executesql N'SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[EnFullName] AS [EnFullName], 
[Extent1].[FatherName] AS [FatherName], 
[Extent1].[GenderID] AS [GenderID], 
[Extent1].[NationalCode] AS [NationalCode], 
[Extent1].[IdentityNumber] AS [IdentityNumber], 
[Extent1].[BirthCityID] AS [BirthCityID], 
[Extent1].[BirthDate] AS [BirthDate], 
[Extent1].[VeteranID] AS [VeteranID], 
[Extent1].[PersonPic] AS [PersonPic], 
[Extent1].[Active] AS [Active], 
[Extent1].[Description] AS [Description], 
[Extent1].[DateTimes] AS [DateTimes]
FROM [dbo].[HRPerson] AS [Extent1]
WHERE [Extent1].[ID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1502

Please help me.请帮我。

i use IQueryable in repo and add Include(u => u.HRPerson) its ok我在 repo 中使用 IQueryable 并添加 Include(u => u.HRPerson) 没问题

var AllList = unitOfWork.Repository<STUser>().GetAll().Include(u => u.HRPerson).ToList();

thankyou @HansKesting for your help谢谢@HansKesting 的帮助

Just a thought I would like to share: (I'm not familiar with that platform)只是我想分享的一个想法:(我不熟悉那个平台)

What about this:那这个呢:

public virtual IEnumerable<YourDataObject> GetAll()
{
    var result = from STUser in unitOfWork.Repository<STUser>().GetAll() // << what is GetAll()??
                 orderby STUser.ID
                 select new YourDataObject // <-- you should create a data holder class for it
                 {
                    STUser.ID,
                    FullName = STUser.HRPerson.LastName,
                    STUser.USR,
                    STUser.Active,
                    STUser.TryCount,
                    STUser.Description
                 });

    return result;
}

By doing the ordering before the creation of the data holder class, you might use an optimalization on the database side.通过在创建数据持有者 class 之前进行排序,您可以在数据库端使用优化。

You could try using.AsNoTracking() on your query.您可以尝试在查询中使用.AsNoTracking()。

From the Microsoft docs -从微软文档 -

No tracking queries are useful when the results are used in a read-only scenario.当结果用于只读方案时,没有跟踪查询很有用。 They're quicker to execute because there's no need to set up the change tracking information.它们执行速度更快,因为无需设置更改跟踪信息。 If you don't need to update the entities retrieved from the database, then a no-tracking query should be used.如果您不需要更新从数据库中检索到的实体,则应使用无跟踪查询。 You can swap an individual query to be no-tracking.您可以将单个查询交换为不跟踪。

More here - https://docs.microsoft.com/en-us/ef/core/querying/tracking更多信息 - https://docs.microsoft.com/en-us/ef/core/querying/tracking

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM