繁体   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