简体   繁体   中英

Why tolist takes too long for only 1 item?

I make a stored procedure call to SQL Server, and I try to get only 1 item. The db returns instantly, however it runs very slow when I try to use 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; }        
}

Stored procedure method:

    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);           
        }
}

DbContext:

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);
    }
}

在此处输入图片说明

Calling the stored procedure:

 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;

The stored procedure in SQL Server returns instantly, so this is not a stored procedure problem (tested directly to run on the database).

As you can see from the point var results to the next command, it takes up to 467ms for only 1 simple item ProductSimple which only contains a few numeric fields, and I tried to move the debug point back and forth to check the speed, it still maintains at the same amount of time.

On the other project, this ToList() converts very fast for the same item structure. What could be the possible wrong things that I make ?

在此处输入图片说明

I'm using ASP.NET Core 2.2.4 and EF Core 2.2.4

The query does not return immediately. The query does not actually execute until the .ToList call.

How long the query takes to return depends on the time to connect to your database as well as the efficiency of the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM