实体框架6和Async()性能

时间:2016-04-27 13:45:44

标签: entity-framework asynchronous

我有关于Entity Framework 6和ToListAsync()/ ToList()的问题。

如果我有少量数据(大约1000行),我注意到ToListAsync()比ToList()更快。

但如果我有大量数据(大约600.000行),ToList()比ToListAsync()更快。

在这里你可以使代码更好,有人可以解释我为什么?。

//SharedAnagra contains around 1000 rows
static List<SharedAnagra> GetSharedAnagraList()
{
    using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
    {
        List<SharedAnagra> listaSharedAnagras = (from a in intranet2K3Entities.sharedAnagra
                                                 select new SharedAnagra
                                                 {
                                                     AnagraActive = a.AnagraActive,
                                                     AnagraId = a.AnagraID,
                                                     AnagraName = a.AnagraName,
                                                     OfficeId = a.OfficeID
                                                 }).ToList();

        return listaSharedAnagras;
    }
}

static async Task<List<SharedAnagra>> GetSharedAnagraListAsync()
{
    using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
    {
        List<SharedAnagra> listaSharedAnagras = await (from a in intranet2K3Entities.sharedAnagra
                                                       select new SharedAnagra
                                                       {
                                                           AnagraActive = a.AnagraActive,
                                                           AnagraId = a.AnagraID,
                                                           AnagraName = a.AnagraName,
                                                           OfficeId = a.OfficeID
                                                       }).ToListAsync();

        return listaSharedAnagras;
    }
} 


//sharedAnagraNDGClienti contains 950000 rows
static List<SharedAnagraNdgClienti> ListaSharedNdgClienti()
{
    using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
    {
        List<SharedAnagraNdgClienti> lista = (from a in intranet2K3Entities.sharedAnagraNDGClienti
                                              select new SharedAnagraNdgClienti
                                              {
                                                  Cm = a.CM,
                                                  CodFisc = a.CodFisc,
                                                  Cognome = a.Cognome,
                                                  Descrizione = a.Descrizione,
                                                  FilialePrevalente = a.Filiale_Prevalente,
                                                  Ndg = a.NDG,
                                                  Nome = a.Nome,
                                                  Telefono = a.Telefono,
                                                  Tipologia = a.Tipologia,
                                                  Trae = a.TRAE,
                                                  Tsae = a.TSAE
                                              }).AsNoTracking().ToList();

        return lista;
    }
}

static async Task<List<SharedAnagraNdgClienti>> GetIssueTypeByIdAsync()
{
    List<SharedAnagraNdgClienti> listaSharedAnagraNdgClienti;

    using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
    {
        listaSharedAnagraNdgClienti = await (from a in intranet2K3Entities.sharedAnagraNDGClienti
                                             select new SharedAnagraNdgClienti
                                             {
                                                 Cm = a.CM,
                                                 CodFisc = a.CodFisc,
                                                 Cognome = a.Cognome,
                                                 Descrizione = a.Descrizione,
                                                 FilialePrevalente = a.Filiale_Prevalente,
                                                 Ndg = a.NDG,
                                                 Nome = a.Nome,
                                                 Telefono = a.Telefono,
                                                 Tipologia = a.Tipologia,
                                                 Trae = a.TRAE,
                                                 Tsae = a.TSAE
                                             }).AsNoTracking().ToListAsync();
    }

    return listaSharedAnagraNdgClienti;
}

1 个答案:

答案 0 :(得分:1)

我认为这不是您的查询问题。这是EF 6+框架本身的问题。我个人没有对Asyn vc Sync做任何研发。但我发现了一些有用的帖子关于那个。希望对你有所帮助。

何时真正使用异步?

  

在大多数使用async的应用程序中都没有明显的好处   甚至可能是有害的。使用测试,分析和常识来   在之前测量特定场景中异步的影响   承诺。

以下是该文章:Async Query & Save (EF6 onwards)

这是Stackoveflow用户之一所进行的伟大研究。你也可以阅读。非常好。

Entity Framework async operation takes ten times as long to complete