簡體   English   中英

為什么 tolist 只為 1 個項目花費太長時間?

[英]Why tolist takes too long for only 1 item?

我對 SQL Server 進行了存儲過程調用,並嘗試僅獲取 1 個項目。 數據庫立即返回,但是當我嘗試使用ToList()時它運行速度非常慢

public partial class Product: BaseEntity, ISlugSupported
{
    public string Name { get; set; }
    public int ManufacturerId { get; set; }       
    public string Sku { get; set; }
    public decimal Price { get; set; }
    public bool InStock { get; set; }
    public int StockQuantity { get; set; }
    public decimal ProductCostInUSD { get; set; }
    public decimal ProductCostInUSDOldPrice { get; set; }
    public decimal ProductCostInPound { get; set; }       
    public decimal ProductCostInEuro { get; set; }     
    public decimal ProductCostInVND { get; set; }
    public bool Published { get; set; }
    public decimal ExchangeRateUSD { get; set; }     
    public decimal ExchangeRateEuro { get; set; }    
    public decimal ExchangeRatePound { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string Createdby { get; set; }

    public int Status { get; set; }
    public int? CategoryId { get; set; }        
}

存儲過程方法:

    public class EFRepository<T> : IRepository<T> where T : BaseEntity
    {
        private readonly ApplicationDbContext _context;
        private DbSet<T> _entities;

        public EFRepository(ApplicationDbContext context)
        {
            this._context = context;            
        }

        public IQueryable<T> ExecuteStoredProcedureList(string commandText, params object[] parameters) 
        {
            return _context.Set<T>().FromSql(commandText, parameters);           
        }
}

數據庫上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        base.OnModelCreating(builder);
        builder.Entity<Product>()
           .ToTable("Product")
           .Ignore(p => p.ProductFinalCost);
    }
}

在此處輸入圖片說明

調用存儲過程:

 var query = _productRepository.ExecuteStoredProcedureList("dbo.[SearchProducts] " +
                  "@Keywords, @PageIndex,@PageSize,@SortBy,@FromDate, @ToDate,@Sku,@CreateBy,@CategoryIds,@OrderIds,@ShowPublished,@Discontinued,@Discount,@LoadFilterableSpecificationAttributeOptionIds, @FilterableSpecificationAttributeOptionIds OUTPUT,@PriceMin,@PriceMax,@ShowExpiredProduct,@FilterableBrands OUTPUT,@TotalRecords OUTPUT,@FilteredSpecs, @FilteredBrands,@LoadSimple,@TrungvangPick",
                  pKeywords,
                  pPageIndex,
                  pPageSize,
                  pSortBy,
                  pFromDate,
                  pToDate,
                  pSku,
                  pCreatedBy,
                  pCategoryIds,
                   pOrderIds,
                  pShowPublished,
                   pDiscontinued,
                   pDiscount,                      
                  pLoadFilterableSpecificationAttributeOptionIds,
                  pFilterableSpecificationAttributeOptionIds,
                  pPriceMin,
                  pPriceMax,
                  pShowExpiredProduct,
                  pFilterableBrands,
                  pTotalRecords,
                  pFilteredSpecs,
                  pFilteredBrands,
                  pLoadSimple,
                  pTrungvangPick
                 );

var result = query.ToList();

int totalRecords = pTotalRecords.Value != DBNull.Value ? Convert.ToInt32(pTotalRecords.Value) : 0;

SQL Server 中的存儲過程立即返回,因此這不是存儲過程問題(在數據庫上直接測試運行)。

正如您從 var 結果點到下一個命令所看到的,僅包含幾個數字字段的 1 個簡單項目ProductSimple最多需要 467 毫秒,我嘗試來回移動調試點以檢查速度,它仍然保持在相同的時間。

在另一個項目中,此ToList()對於相同的項目結構轉換非常快。 我可能做錯了什么?

在此處輸入圖片說明

我正在使用 ASP.NET Core 2.2.4 和 EF Core 2.2.4

查詢不會立即返回。 在 .ToList 調用之前,查詢實際上不會執行。

查詢返回所需的時間取決於連接到數據庫的時間以及查詢的效率。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM