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
+51
-25
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,17 +4133,32 @@ 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}'");
|
||||
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"]);
|
||||
@@ -4145,7 +4172,6 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5) 组装结果
|
||||
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
|
||||
|
||||
@@ -158,7 +158,7 @@ body { font-family: 'Microsoft YaHei','PingFang SC',sans-serif; background: #f3f
|
||||
<span>风险:</span>
|
||||
<select id="filterRisk">
|
||||
<option value="">全部</option>
|
||||
<option value="断线">断线</option>
|
||||
<option value="断线" selected>断线</option>
|
||||
<option value="预警">预警</option>
|
||||
<option value="安全">安全</option>
|
||||
</select>
|
||||
@@ -192,7 +192,7 @@ const DAY_WIDTH = 56; // 每天宽度(按截图样式,约56px/列)
|
||||
const ROW_HEIGHT = 50; // 每行高度
|
||||
const HEAD_HEIGHT = 36; // 表头高度
|
||||
let autoRefreshTimer = null;
|
||||
let currentRisk = '';
|
||||
let currentRisk = '断线'; // 默认只加载断线风险物料(957条,2.5s),空筛选拉15711条=浏览器灾难
|
||||
|
||||
function parseDate(s) {
|
||||
if (!s) return null;
|
||||
@@ -220,12 +220,11 @@ function loadSummary() {
|
||||
}
|
||||
|
||||
function loadProductTypes() {
|
||||
fetch(API_BASE + '/GetSchedules?keyword=').then(r => r.json()).then(res => {
|
||||
if (res.success) {
|
||||
// 用 GetOptions 接口(已验证快且正确),而非不存在的 GetSchedules
|
||||
fetch(API_BASE + '/GetOptions?field=productType').then(r => r.json()).then(res => {
|
||||
if (res.success && res.data && res.data.productTypes) {
|
||||
const sel = document.getElementById('filterProductType');
|
||||
const set = new Set();
|
||||
res.data.forEach(s => { if (s.ProductType) set.add(s.ProductType); });
|
||||
[...set].sort().forEach(t => {
|
||||
res.data.productTypes.slice().sort().forEach(t => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = t; opt.textContent = t;
|
||||
sel.appendChild(opt);
|
||||
@@ -239,6 +238,7 @@ function loadProductTypes() {
|
||||
function loadGantt() {
|
||||
document.getElementById('ganttContainer').innerHTML = '<div class="loading">加载中...</div>';
|
||||
const params = new URLSearchParams();
|
||||
params.append('onlyToday', 'true'); // 限制最多7天,砍数据量
|
||||
// 优先从 MultiSelect 组件读多值,fallback 原生 .value
|
||||
const ptEl = document.getElementById('filterProductType');
|
||||
const ptCsv = ptEl.getMultiValues ? ptEl.getMultiValues().join(',') : (ptEl.value || '');
|
||||
|
||||
Reference in new issue
Block a user