由于Entity Framework(连接到数据库),CPU使用率上升

时间:2021-01-18 02:13:48

I have website (2 core-6gb ram), and for every request that goes to server cpu start to go up and when request is answered cpu came to normal

我有网站(2核-6gb ram),并且对于服务器cpu的每个请求开始上升并且当请求被回答时cpu变为正常

And I test that in simple console application, and again it has happened

我在简单的控制台应用程序中测试它,并且它再次发生了

I found out that is because of Entity Framework (add-update-delete-find ,..), so what should I do? I do everything right....

我发现这是因为实体框架(add-update-delete-find,..),所以我该怎么办?我做对了一切......

This my repository sample:

这是我的存储库示例:

public class FactorRepository : IDisposable
{
    private DomainModels.AppEntities _dbDnt = null;

    public FactorRepository()
    {
        _dbDnt = new DomainModels.AppEntities();
    }
    public bool Add(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Factors.Add(entity);
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Update(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Factors.Attach(entity);
            _dbDnt.Entry(entity).State = EntityState.Modified;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Entry(entity).State = EntityState.Deleted;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(long id, bool autoSave = true)
    {
        try
        {
            var entity = _dbDnt.Factors.Find(id);
            _dbDnt.Entry(entity).State = EntityState.Deleted;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public DomainModels.Factor Find(long id)
    {
        try
        {
            return _dbDnt.Factors.Find(id);
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Where(Expression<Func<DomainModels.Factor, bool>> predicate)
    {
        try
        {
            return _dbDnt.Factors.Where(predicate).ToList();
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Select()
    {
        try
        {
            return _dbDnt.Factors.AsQueryable().ToList();
        }
        catch
        {
            return null;
        }
    }

    public List<TResult> Select<TResult>(Expression<Func<DomainModels.Factor, TResult>> selector)
    {
        try
        {
            return _dbDnt.Factors.Select(selector).ToList();
        }
        catch
        {
            return null;
        }
    }

    public long GetLastIdentity()
    {
        try
        {
            if (_dbDnt.Factors.Any())
                return _dbDnt.Factors.OrderByDescending(p => p.Id).First().Id;
            else
                return 0;
        }
        catch
        {
            return -1;
        }
    }

    public long Save()
    {
        try
        {
            return _dbDnt.SaveChanges();
        }
        catch
        {
            return -1;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this._dbDnt != null)
            {
                this._dbDnt.Dispose();
                this._dbDnt = null;
            }
        }
    }

    ~FactorRepository()
    {
        Dispose(false);
    }

Update: even when I use this class as repository problem still ongoing

更新:即使我使用此类作为存储库问题仍在进行中

    public class FactorRepository 
{
    public bool Add(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Factors.Add(entity);
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Update(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Factors.Attach(entity);
                database.Entry(entity).State = EntityState.Modified;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Entry(entity).State = EntityState.Deleted;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(int id, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                var entity = database.Factors.Find(id);
                database.Entry(entity).State = EntityState.Deleted;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public DomainModels.Factor Find(long id)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Find(id);
            }
        }
        catch
        {
            return null;
        }
    }

    public DomainModels.Factor Find(string username)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Find(username);
            }
        }
        catch
        {
            return null;
        }
    }
    public List<DomainModels.Factor> Where(Expression<Func<DomainModels.Factor, bool>> predicate)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Where(predicate).ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Select()
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.AsQueryable().ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public List<TResult> Select<TResult>(Expression<Func<DomainModels.Factor, TResult>> selector)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Select(selector).ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public long GetLastIdentity()
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                if (database.Factors.Any())
                    return database.Factors.OrderByDescending(p => p.Id).First().Id;
                else
                    return 0;
            }

        }
        catch
        {
            return -1;
        }
    }
}

Update: usage

FactorRepository blFact = new FactorRepository();

var model1 = blFact.Where(p => p.UserId == userid).SingleOrDefault();

1 个解决方案

#1


0  

Finding performance issues is kinda hard, but I think that in your case the query compilation phase from an Entity Framework is a problem. You can very quickly verify it using a Performance Monitor tool from Windows system and PerfView (or any other memory profiler).

查找性能问题有点困难,但我认为在您的情况下,实体框架中的查询编译阶段是一个问题。您可以使用Windows系统和PerfView(或任何其他内存分析器)中的性能监视器工具快速验证它。

Open a performance monitor and add the following counters to your process: .NET CLR Memory (% Time in GC and Allocated Bytes/sec). If you have a lot of allocations or your time in GC is over > 10 then we are closer to confirm that fault is in the query compilation phase. Now execute PerfView to monitor your memory usage. If in the result you notice a lot of allocation from the entity framework namespace then yes it's a query compilation phase.

打开性能监视器并将以下计数器添加到您的进程:.NET CLR内存(GC中的时间百分比和分配的字节数/秒)。如果您有大量的分配或GC中的时间超过> 10,那么我们更接近确认故障是在查询编译阶段。现在执行PerfView来监视内存使用情况。如果在结果中你注意到实体框架命名空间的大量分配,那么它是一个查询编译阶段。

I have a similar issue, you can take a look here: Entity framework 6.1.3 high cpu usage for Insert, Update operations

我有一个类似的问题,你可以看看这里:实体框架6.1.3插入,更新操作的高CPU使用率

Additionally, I wrote a blog post about the query compilation phase and problems I had on a read side: http://devmate.net/2017/10/entity-framework-the-query-plan-cache-story/. Maybe it will be handy for you too.

另外,我写了一篇关于查询编译阶段的博客文章以及我在阅读方面遇到的问题:http://devmate.net/2017/10/entity-framework-the-query-plan-cache-story/。也许它对你来说也很方便。

#1


0  

Finding performance issues is kinda hard, but I think that in your case the query compilation phase from an Entity Framework is a problem. You can very quickly verify it using a Performance Monitor tool from Windows system and PerfView (or any other memory profiler).

查找性能问题有点困难,但我认为在您的情况下,实体框架中的查询编译阶段是一个问题。您可以使用Windows系统和PerfView(或任何其他内存分析器)中的性能监视器工具快速验证它。

Open a performance monitor and add the following counters to your process: .NET CLR Memory (% Time in GC and Allocated Bytes/sec). If you have a lot of allocations or your time in GC is over > 10 then we are closer to confirm that fault is in the query compilation phase. Now execute PerfView to monitor your memory usage. If in the result you notice a lot of allocation from the entity framework namespace then yes it's a query compilation phase.

打开性能监视器并将以下计数器添加到您的进程:.NET CLR内存(GC中的时间百分比和分配的字节数/秒)。如果您有大量的分配或GC中的时间超过> 10,那么我们更接近确认故障是在查询编译阶段。现在执行PerfView来监视内存使用情况。如果在结果中你注意到实体框架命名空间的大量分配,那么它是一个查询编译阶段。

I have a similar issue, you can take a look here: Entity framework 6.1.3 high cpu usage for Insert, Update operations

我有一个类似的问题,你可以看看这里:实体框架6.1.3插入,更新操作的高CPU使用率

Additionally, I wrote a blog post about the query compilation phase and problems I had on a read side: http://devmate.net/2017/10/entity-framework-the-query-plan-cache-story/. Maybe it will be handy for you too.

另外,我写了一篇关于查询编译阶段的博客文章以及我在阅读方面遇到的问题:http://devmate.net/2017/10/entity-framework-the-query-plan-cache-story/。也许它对你来说也很方便。