SSMS即时时,实体框架非常慢

时间:2018-09-12 16:28:52

标签: c# entity-framework

由于某些奇怪的原因,当我在Visual Studio 2017中查询表时,它花费的时间太长,并且用户感到沮丧。它正在返回25,000条记录。  ssms上的同一段代码是即时的。

这是我的EF代码。

   var sw = new System.Diagnostics.Stopwatch
   sw.Start();
   var map  = ce.myTable.ToList();
   sw.Stop();
   var se = sw.Elapsed.Seconds;

通过秒表计数需要24秒

如果我在SSMS上运行,从EF查询中提取的相同代码将花费1秒。

SELECT 
    [Extent1].[RefDataChangeEventId] AS [RefDataChangeEventId], 
    [Extent1].[InvestNum] AS [InvestNum], 
    [Extent1].[OldInvestNum] AS [OldInvestNum], 
    [Extent1].[InvestName] AS [InvestName], 
    [Extent1].[InvestAddr] AS [InvestAddr], 
    [Extent1].[InvestCity] AS [InvestCity], 
    [Extent1].[PhoneNum] AS [PhoneNum], 
    [Extent1].[ContactPerson] AS [ContactPerson], 
    [Extent1].[CustodialBankName] AS [CustodialBankName], 
    [Extent1].[CustodialBankCityState] AS [CustodialBankCityState], 
    [Extent1].[RoutingNum] AS [RoutingNum], 
    [Extent1].[CheckingAcctNum] AS [CheckingAcctNum], 
    [Extent1].[EscrowAcctNum] AS [EscrowAcctNum], 
    [Extent1].[EscrowBankName] AS [EscrowBankName], 
    [Extent1].[EscrowBankCityState] AS [EscrowBankCityState], 
    [Extent1].[InvestAgencyType] AS [InvestAgencyType], 
    [Extent1].[ConformUsingInvestNumFlag] AS [ConformUsingInvestNumFlag], 
    [Extent1].[InvestAcctingRptStyleType] AS [InvestAcctingRptStyleType], 
    [Extent1].[SchedBalCutoffType] AS [SchedBalCutoffType], 
    [Extent1].[PmtType] AS [PmtType], 
    [Extent1].[PrinRemitType] AS [PrinRemitType], 
    [Extent1].[IntRemitType] AS [IntRemitType], 
    [Extent1].[DlvyType] AS [DlvyType], 
    [Extent1].[SellerServicerNum] AS [SellerServicerNum], 
    [Extent1].[GovtFlag] AS [GovtFlag], 
    [Extent1].[BondREOType] AS [BondREOType], 
    [Extent1].[NegAmortFlag] AS [NegAmortFlag], 
    [Extent1].[MultiFamilyFlag] AS [MultiFamilyFlag], 
    [Extent1].[SrvType] AS [SrvType], 
    [Extent1].[AcqFlag] AS [AcqFlag], 
    [Extent1].[RemitDt] AS [RemitDt], 
    [Extent1].[RemitDayType] AS [RemitDayType], 
    [Extent1].[RemitStyleType] AS [RemitStyleType], 
    [Extent1].[MajorFlag] AS [MajorFlag], 
    [Extent1].[CoIssueFlag] AS [CoIssueFlag], 
    [Extent1].[ParticipationConduitFlag] AS [ParticipationConduitFlag], 
    [Extent1].[HedgeableInvFlagBit] AS [HedgeableInvFlagBit], 
    [Extent1].[TreasBankPortfolioFlagBit] AS [TreasBankPortfolioFlagBit], 
    [Extent1].[TreasBankPortfolioToCHLFlagBit] AS [TreasBankPortfolioToCHLFlagBit], 
    [Extent1].[UnsaleableInvFlagBit] AS [UnsaleableInvFlagBit], 
    [Extent1].[PoolableInvFlagBit] AS [PoolableInvFlagBit], 
    [Extent1].[AcctingInvFlagBit] AS [AcctingInvFlagBit], 
    [Extent1].[InvAvailForRepoFlagBit] AS [InvAvailForRepoFlagBit], 
    [Extent1].[RepurchaseInvAvailForRepoFlagBit] AS [RepurchaseInvAvailForRepoFlagBit], 
    [Extent1].[MtgVenturesFlagBit] AS [MtgVenturesFlagBit], 
    [Extent1].[CSCPortfolioFlagBit] AS [CSCPortfolioFlagBit], 
    [Extent1].[REOFlagBit] AS [REOFlagBit], 
    [Extent1].[OTCFlagBit] AS [OTCFlagBit], 
    [Extent1].[LegalOwnerId] AS [LegalOwnerId], 
    [Extent1].[ManagedByCd] AS [ManagedByCd], 
    [Extent1].[HFSHFICd] AS [HFSHFICd], 
    [Extent1].[NewInvestNumFlag] AS [NewInvestNumFlag], 
    [Extent1].[LoanExistFlagBit] AS [LoanExistFlagBit], 
    [Extent1].[SBOFlagBit] AS [SBOFlagBit], 
    [Extent1].[MICompanyFlagBit] AS [MICompanyFlagBit], 
    [Extent1].[PremPrivInvTypeCd] AS [PremPrivInvTypeCd], 
    [Extent1].[SecuritizeHFIFlagBit] AS [SecuritizeHFIFlagBit], 
    [Extent1].[ExecutionTypeDesc] AS [ExecutionTypeDesc], 
    [Extent1].[InvestContractName] AS [InvestContractName], 
    [Extent1].[SrvInvestGrpType] AS [SrvInvestGrpType], 
    [Extent1].[UpdatedOn] AS [UpdatedOn], 
    [Extent1].[UpdatedBy] AS [UpdatedBy], 
    [Extent1].[ActiveFlagBit] AS [ActiveFlagBit], 
    [Extent1].[DataDt] AS [DataDt], 
    [Extent1].[BegDt] AS [BegDt], 
    [Extent1].[EndDt] AS [EndDt], 
    [Extent1].[CurrentFlagBit] AS [CurrentFlagBit], 
    [Extent1].[LTSBFlag] AS [LTSBFlag], 
    [Extent1].[InsurerType] AS [InsurerType], 
    [Extent1].[RESIInvestNumFlag] AS [RESIInvestNumFlag], 
    [Extent1].[GLCompany] AS [GLCompany], 
    [Extent1].[GLCostCenter] AS [GLCostCenter], 
    [Extent1].[GLProfitCenter] AS [GLProfitCenter], 
    [Extent1].[HFSTransferFlagBit] AS [HFSTransferFlagBit]
    FROM [dbo].[myTable] AS [Extent1]

这里可能是什么问题?

2 个答案:

答案 0 :(得分:4)

如果您没有使用全部25k条记录,请立即避免使用ToList()进行过滤/分页,然后对结果进行ToList()并将其返回。抓取整个表并对其进行ToList()处理25k条记录肯定会花费一些时间,因此,如果您出于查询目的而减少了记录的数量(您是否实际上返回了全部25k条?),这将有所帮助

这里也有很多有关性能调整的好信息:https://docs.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper

答案 1 :(得分:0)

是的,要录制25k记录很多。在SSMS中,您只是查询数据并显示它,在Entity Framework中,您是在创建所述表的实体对象。如果确实需要查询25k条记录,则可以尝试拆分数据,一次查询100条左右的记录。