【发布时间】:2020-05-15 23:10:36
【问题描述】:
我的目标是提供简单的 API 来从包含 5 列的 Payments(约 400 行)表中检索数据。
Payment: Id (int),
PaymentsNumber (tinyint),
Rate (decimal(18,2)),
ProductType (tinyint),
ClientClubType (tinyint).
用户可以使用以下请求参数发出帖子请求(应该返回约 12 行):
PaymentsRequest
{
public int? PaymentsNumber { get; set; }
public byte? ProductType { get; set; }
public byte? ClientClubType { get; set; }
}
使用 EF-Core:
services.AddDbContext<MyContext>(cfg => cfg.UseSqlServer(Configuration.GetConnectionString(...),optionsBuilder => optionsBuilder.CommandTimeout(60)));
public async Task<IEnumerable<Payments>> GetPaymentsAsync(PaymentsRequest request)
{
IQueryable<Payments> query = this._context.Set<Payments>();
query = query.Where(filter =>
(request.ClientClubType == null || filter.ClientClubType == request.ClientClubType) &&
(request.ProductType == null || filter.ProductType == request.ProductType) &&
(request.PaymentsNumber == null || filter.PaymentsNumber == request.PaymentsNumber));
return await query.ToListAsync();
}
在 Azure 应用程序洞察中,我可以看到 2 个连续日志,由同一异常引起:
- Log1:执行 DbCommand 失败。
- Log2:执行超时已过期。在操作完成之前超时时间已过或服务器没有响应。
Log1是(虽然这里不需要写log2):
执行 DbCommand 失败(65,238 毫秒) [参数=[@__request_ClientClubType_0='?' (大小 = 1)(DbType = 字节),@__request_ProductType_1='?' (大小 = 1) (DbType = 字节)], CommandType='Text', CommandTimeout='60']
选择 [p].[Id], [p].[ClientClubType], [p].[PaymentsNumber], [p].[ProductType], [p].[Rate] FROM [Payments] AS [p] WHERE (([p].[ClientClubType] = @__request_ClientClubType_0) AND @__request_ClientClubType_0 不是 NULL) AND (([p].[ProductType] = @__request_ProductType_1) AND @__request_ProductType_1 不为空)
我的应用程序是部署在 azure linux webapp 上的 .net core 3.0 应用程序。
该问题仅在生产中出现,并非每次都出现,我无法从 MSSMS 中重建问题。 有什么想法吗?
更新:
@panagiotis-kanavos 发表评论后,我已将代码更新为:
services.AddDbContextPool<MyContext>(cfg => cfg.UseSqlServer(Configuration.GetConnectionString(...),optionsBuilder => optionsBuilder.CommandTimeout(60)));
public async Task<IEnumerable<Payments>> GetPaymentsAsync(PaymentsRequest request)
{
IQueryable<Payments> query = this._context.Payments;
query = query.Where(filter =>
(filter.ClientClubType == request.ClientClubType) &&
(filter.ProductType == request.ProductType) &&
(filter.PaymentsNumber == request.PaymentsNumber));
return await query.ToListAsync();
}
【问题讨论】:
-
至于为什么要花这么长时间,可能是缺少索引,与其他查询的并发冲突,查询本身的问题(catch-all 查询不好,LINQ 根本不需要)。最有可能的是,由于
NULLs,查询锁定了太多行,从而导致与尝试访问同一张表的其他查询发生冲突。也许也是一个长期运行的交易? -
您可以使用
if(someParam!=null){ query=query.Where(r=>r.field==param);}动态添加条件。这个Wheres 序列等价于AND。如果不需要参数,就不要加条件 -
该表扫描现在对所有行使用 S(hared) 锁。这意味着任何想要更新的查询都必须等待其他事务完成。如果你的代码显式打开并持有一个事务(它不应该与任何 ORM 一起做),锁可能会保留很长时间。这增加了冲突并阻塞了 lot
-
如果您使用数据库事务来实现“每个请求的业务事务”或您不需要的工作单元,那么您的查询最终会相互阻塞。您不需要这样做,因为 ORM 的 cache 更改并且仅在调用
SaveChanges时以原子方式将它们保存在一个批次中。你已经有一个 UoW,你不需要 DB 事务来实现它(反正不能这样做) -
您能否安装 sp_whoisactive 并检查您在查询运行时有多少事务?
标签: c# sql-server azure .net-core entity-framework-core