Browse Source

定位入库性能问题为查询余额表累计时间过长

master
wanggang 1 year ago
parent
commit
2b6ccd8d6a
  1. 39
      code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs

39
code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs

@ -4,11 +4,11 @@ using System.Data.SqlClient;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using EFCore.BulkExtensions; using EFCore.BulkExtensions;
using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using Magicodes.ExporterAndImporter.Core.Extension; using Magicodes.ExporterAndImporter.Core.Extension;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -56,7 +56,6 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
return (DateTime.Now - now).TotalMinutes; return (DateTime.Now - now).TotalMinutes;
} }
[NonAction] [NonAction]
public async Task Invoke(IServiceProvider serviceProvider) public async Task Invoke(IServiceProvider serviceProvider)
{ {
@ -78,6 +77,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
var connectionString = serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService"); var connectionString = serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService");
for (var i = 0; i < 1000; i++) for (var i = 0; i < 1000; i++)
{ {
//1:新建 2:更新
var balanceList = new List<Tuple<VmiBalance, int>>(); var balanceList = new List<Tuple<VmiBalance, int>>();
var vmiReplenishedList = new List<VmiReplenished>(); var vmiReplenishedList = new List<VmiReplenished>();
var sw = new Stopwatch(); var sw = new Stopwatch();
@ -95,7 +95,13 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
var vmiMessageRepo = context.Set<VmiMessage>(); var vmiMessageRepo = context.Set<VmiMessage>();
var vmiLogRepo = context.Set<VmiLog>(); var vmiLogRepo = context.Set<VmiLog>();
var vmiBalanceRepo = context.Set<VmiBalance>(); var vmiBalanceRepo = context.Set<VmiBalance>();
var vmiReplenishedRepo = context.Set<VmiReplenished>(); //sw.Stop();
//this._logger.LogInformation($"缓存开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
//var balanceCachedList = vmiBalanceRepo.AsNoTracking().ToList();
//sw.Stop();
//this._logger.LogInformation($"缓存结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
//读取可消费消息列表 //读取可消费消息列表
var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList(); var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList();
//没有可消费消息则返回 //没有可消费消息则返回
@ -132,6 +138,9 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
} }
//sw.Stop();
//this._logger.LogInformation($"计时:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
foreach (var item in list) foreach (var item in list)
{ {
var message = item.VmiMessage; var message = item.VmiMessage;
@ -147,11 +156,16 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
o.OrderNum == log.OrderNum && o.OrderNum == log.OrderNum &&
o.factory == log.factory && o.factory == log.factory &&
o.Configcode == log.Configcode); o.Configcode == log.Configcode);
//sw.Stop();
////this._logger.LogInformation($"计时:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
//数据库查找 //数据库查找
if (balance == null) if (balance == null)
{ {
balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault( //sw.Stop();
////this._logger.LogInformation($"查询开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault(// balanceCachedList.Find(
o => o.DeliverBillType == log.DeliverBillType && o => o.DeliverBillType == log.DeliverBillType &&
o.CodeType == log.CodeType && o.CodeType == log.CodeType &&
o.RealPartCode == log.RealPartCode && o.RealPartCode == log.RealPartCode &&
@ -160,6 +174,9 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
o.OrderNum == log.OrderNum && o.OrderNum == log.OrderNum &&
o.factory == log.factory && o.factory == log.factory &&
o.Configcode == log.Configcode); o.Configcode == log.Configcode);
//sw.Stop();
//this._logger.LogInformation($"查询结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
} }
if (balance == null) if (balance == null)
{//不存在库存记录 {//不存在库存记录
@ -227,14 +244,14 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
} }
// 更新库存 // 更新库存
balance.Qty = currentQty + log.ChangedQty; balance.Qty = currentQty + log.ChangedQty;
if (balance.Qty == decimal.Zero)
{
//删除0库存
vmiBalanceRepo.Remove(balance);
}
} }
//sw.Stop();
////this._logger.LogInformation($"准备1:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000 / 60}分钟,{sw.ElapsedMilliseconds / 1000}秒");
//sw.Restart();
} }
//sw.Stop();
//this._logger.LogInformation($"准备{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000 / 60}分钟,{sw.ElapsedMilliseconds / 1000}秒");
//sw.Restart();
//更新事务分表 //更新事务分表
foreach (var item in tables) foreach (var item in tables)
{ {

Loading…
Cancel
Save