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:
Gaussian committed 2026-11-10 14:22:50 +08:00
1 parent 6dab7ce1b0
commit 4a4214f075
2 files changed
+61 -35

No files matched your search

+7 -7
View File
@@ -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 || '');