简体   繁体   English

为什么 tolist 只为 1 个项目花费太长时间?

[英]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.我对 SQL Server 进行了存储过程调用,并尝试仅获取 1 个项目。 The db returns instantly, however it runs very slow when I try to use ToList()数据库立即返回,但是当我尝试使用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). SQL Server 中的存储过程立即返回,因此这不是存储过程问题(在数据库上直接测试运行)。

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.正如您从 var 结果点到下一个命令所看到的,仅包含几个数字字段的 1 个简单项目ProductSimple最多需要 467 毫秒,我尝试来回移动调试点以检查速度,它仍然保持在相同的时间。

On the other project, this ToList() converts very fast for the same item structure.在另一个项目中,此ToList()对于相同的项目结构转换非常快。 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我正在使用 ASP.NET Core 2.2.4 和 EF Core 2.2.4

The query does not return immediately.查询不会立即返回。 The query does not actually execute until the .ToList call.在 .ToList 调用之前,查询实际上不会执行。

How long the query takes to return depends on the time to connect to your database as well as the efficiency of the query.查询返回所需的时间取决于连接到数据库的时间以及查询的效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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