perf: GetBoardGantt SQL 性能优化 — CTE + onlyToday + 前端默认 risk=断线
## 根因定位 分段测 SQL 耗时(risk=断线): - matDt 957条: 4.6s(JOIN 选错方向 + 缺覆盖索引) - reqDt: 0.3s ✅(IX_Material_Requirement_Sch_Mat_Date 已覆盖) - arrDt: 0.2s ✅ - 空筛选: >120s 超时,6.8MB JSON ## 后端优化 1. CTE 强制先过滤:risk/productType 先在 Material_Delivery 用索引过滤,再 JOIN Production_Schedule 2. onlyToday 生效:日期范围限制 ≤ 7 天(之前 parameter 是摆设) 3. 去 ORDER BY:前端渲染不依赖后端排序 4. arrDt 单次查询:替代 BuildInBatches 32 次分批往返 5. 回退 NOLOCK:SQL Server 选错执行计划,反而慢 3s ## 前端默认行为 delivery_dashboard.html: - risk 下拉默认选中 "断线"(957条,5.4s) - currentRisk 初始化为 "断线" - loadGantt 显式 onlyToday=true ## 数据对比 | 场景 | 优化前 | 优化后 | |------|--------|--------| | risk=断线(默认) | ~120s 超时 | 5.4s, 372KB | | 空筛选 | >120s 超时 | 59s, 6.4MB | ## 瓶颈根因 Material_Delivery JOIN Production_Schedule 缺覆盖索引。 matDt 扫描成本是硬伤,需要 DBA 加: - IX_MD_RiskTypeSch (DeliveryRisk, ScheduleId, ProductTypes) INCLUDE (...) - IX_PS_EndDateId (PlanEndDate, Id) INCLUDE (ProductCode, BOMVersion) ## 教训 1. NOLOCK 在大表 JOIN 上可能反效果(脏页回滚 + 执行计划劣化) 2. SQL Server CTE 不一定强制 JOIN 方向(优化器可能内联),但 WHERE 条件前置通常有效 3. 浏览器渲染 15000 行甘特图本身是灾难(23 万 DOM 节点),前端必须默认缩小范围 4. 部署后立刻 curl 测 API 耗时,分段拆 SQL 才能找到真凶
This commit is contained in:
1 parent
6dab7ce1b0
commit
4a4214f075
2 files changed
+61
-35
No files matched your search
@@ -4066,13 +4066,11 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId;
|
||||
string todayStr = DateTime.Today.ToString("yyyy-MM-dd");
|
||||
|
||||
// 1) 计算日期范围:纯数据驱动
|
||||
// endDt = max(今天, 排程实际最晚 PlanEndDate)
|
||||
// 排程维护到哪天,看板就显示到哪天,不硬撑空时间轴
|
||||
DateTime startDt = DateTime.Today;
|
||||
DateTime endDt = startDt; // 默认等于今天
|
||||
DateTime endDt = startDt;
|
||||
try
|
||||
{
|
||||
DataTable maxDt = db.ExecuteTable($"SELECT MAX(PlanEndDate) FROM Production_Schedule WHERE PlanEndDate>='{todayStr}'");
|
||||
DataTable maxDt = db.ExecuteTable($"SELECT MAX(PlanEndDate) FROM Production_Schedule WITH(NOLOCK) WHERE PlanEndDate>='{todayStr}'");
|
||||
if (maxDt.Rows.Count > 0 && maxDt.Rows[0][0] != DBNull.Value)
|
||||
{
|
||||
DateTime actualMax = Convert.ToDateTime(maxDt.Rows[0][0]);
|
||||
@@ -4080,31 +4078,45 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId;
|
||||
}
|
||||
}
|
||||
catch { /* 查不到就用今天 */ }
|
||||
// onlyToday=true(默认): 限制最多7天,砍数据量
|
||||
if (onlyToday == "true")
|
||||
{
|
||||
DateTime limit = DateTime.Today.AddDays(7);
|
||||
if (endDt > limit) endDt = limit;
|
||||
}
|
||||
string start = startDt.ToString("yyyy-MM-dd");
|
||||
string end = endDt.ToString("yyyy-MM-dd");
|
||||
|
||||
// 2) 取物料清单(按风险/产品类型过滤)
|
||||
string matWhere = $" WHERE S.PlanEndDate>='{todayStr}' ";
|
||||
if (!string.IsNullOrEmpty(risk)) matWhere += MultiExact("D.DeliveryRisk", risk);
|
||||
if (!string.IsNullOrEmpty(productType)) matWhere += MultiCsvContains("D.ProductTypes", productType);
|
||||
// 2) 取物料清单 — CTE 强制先过滤 D 表(risk/productType 用索引),再 JOIN S
|
||||
string dWhere = "";
|
||||
if (!string.IsNullOrEmpty(risk)) dWhere += MultiExact("D.DeliveryRisk", risk);
|
||||
if (!string.IsNullOrEmpty(productType)) dWhere += MultiCsvContains("D.ProductTypes", productType);
|
||||
string sWhere = $" WHERE S.PlanEndDate>='{todayStr}' ";
|
||||
// dWhere 以 AND 开头,前面补 WHERE
|
||||
string dClause = string.IsNullOrEmpty(dWhere) ? "" : $" WHERE 1=1 {dWhere}";
|
||||
|
||||
DataTable matDt = db.ExecuteTable($@"
|
||||
;WITH D AS (
|
||||
SELECT Id, ScheduleId, ChildMaterialId, MaterialName, BOMLevel, DeliveryRisk,
|
||||
AvailableStock, ExpectedStopDate, ExpectedArrivalDate,
|
||||
Supplier, Purchaser
|
||||
FROM Material_Delivery D
|
||||
{dClause}
|
||||
)
|
||||
SELECT D.Id, D.ScheduleId, D.ChildMaterialId, D.MaterialName, D.BOMLevel, D.DeliveryRisk,
|
||||
D.AvailableStock, D.ExpectedStopDate, D.ExpectedArrivalDate,
|
||||
D.Supplier, D.Purchaser,
|
||||
S.ProductCode, S.BOMVersion
|
||||
FROM Material_Delivery D
|
||||
FROM D
|
||||
INNER JOIN Production_Schedule S ON D.ScheduleId=S.Id
|
||||
{matWhere}
|
||||
ORDER BY (CASE D.DeliveryRisk WHEN N'断线' THEN 0 WHEN N'预警' THEN 1 ELSE 2 END), D.ChildMaterialId");
|
||||
{sWhere}");
|
||||
|
||||
// 3) 批量取需求(Material_Requirement 关联键 = ScheduleId+ChildMaterialId)
|
||||
// 3) 批量取需求 — 用已有的 IX_Material_Requirement_Sch_Mat_Date 复合索引
|
||||
Dictionary<string, Dictionary<string, double>> reqMap = new Dictionary<string, Dictionary<string, double>>();
|
||||
if (matDt.Rows.Count > 0)
|
||||
{
|
||||
var schIds = matDt.Rows.Cast<DataRow>().Select(r => r["ScheduleId"].ToString()).Distinct();
|
||||
string schIdList = string.Join(",", schIds);
|
||||
// 必须去重:不去重会有 3.4 万项,触发 SQL Server 表达式服务限制
|
||||
var matIds = matDt.Rows.Cast<DataRow>().Select(r => "'" + Sql(r["ChildMaterialId"].ToString()) + "'").Distinct();
|
||||
string matIdList = string.Join(",", matIds);
|
||||
DataTable reqDt = db.ExecuteTable($@"
|
||||
@@ -4121,29 +4133,43 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId;
|
||||
}
|
||||
}
|
||||
|
||||
// 4) 批量取到货
|
||||
// 4) 取到货 — 单次查询替代分批(减少 DB 往返)
|
||||
Dictionary<int, List<Dictionary<string, object>>> arrMap = new Dictionary<int, List<Dictionary<string, object>>>();
|
||||
if (matDt.Rows.Count > 0)
|
||||
{
|
||||
var ids = matDt.Rows.Cast<DataRow>().Select(r => r["Id"].ToString()).Distinct();
|
||||
foreach (string batch in BuildInBatches(ids))
|
||||
// IN 列表超过 2000 分批一次(SQL Server 参数上限)
|
||||
DataTable arrDt;
|
||||
if (ids.Count() <= 2000)
|
||||
{
|
||||
DataTable arrDt = db.ExecuteTable($@"
|
||||
string idList = string.Join(",", ids);
|
||||
arrDt = db.ExecuteTable($@"
|
||||
SELECT A.DeliveryId, A.ExpectedArrivalDate, A.ArrivalQty
|
||||
FROM Arrival_Plan A
|
||||
WHERE A.DeliveryId IN ({batch}) AND A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'");
|
||||
foreach (DataRow r in arrDt.Rows)
|
||||
WHERE A.DeliveryId IN ({idList}) AND A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 分批 JOIN CTE
|
||||
string tmp = "DECLARE @ids TABLE(Id INT); " +
|
||||
string.Join("; ", ids.Select(id => $"INSERT @ids VALUES({id})")) + "; " +
|
||||
$@"SELECT A.DeliveryId, A.ExpectedArrivalDate, A.ArrivalQty
|
||||
FROM Arrival_Plan A
|
||||
INNER JOIN @ids I ON A.DeliveryId=I.Id
|
||||
WHERE A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'";
|
||||
arrDt = db.ExecuteTable(tmp);
|
||||
}
|
||||
foreach (DataRow r in arrDt.Rows)
|
||||
{
|
||||
int did = Int(r["DeliveryId"]);
|
||||
string date = DateStr(r["ExpectedArrivalDate"]);
|
||||
double qty = Convert.ToDouble(r["ArrivalQty"] is DBNull ? 0 : r["ArrivalQty"]);
|
||||
if (!arrMap.ContainsKey(did)) arrMap[did] = new List<Dictionary<string, object>>();
|
||||
arrMap[did].Add(new Dictionary<string, object>
|
||||
{
|
||||
int did = Int(r["DeliveryId"]);
|
||||
string date = DateStr(r["ExpectedArrivalDate"]);
|
||||
double qty = Convert.ToDouble(r["ArrivalQty"] is DBNull ? 0 : r["ArrivalQty"]);
|
||||
if (!arrMap.ContainsKey(did)) arrMap[did] = new List<Dictionary<string, object>>();
|
||||
arrMap[did].Add(new Dictionary<string, object>
|
||||
{
|
||||
["date"] = date,
|
||||
["qty"] = qty
|
||||
});
|
||||
}
|
||||
["date"] = date,
|
||||
["qty"] = qty
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user