fix: 分区明细全量查询去掉 ORDER BY,避免查价 sort_buffer 溢出
All checks were successful
Build and Deploy / deploy (push) Successful in 1m1s

大分区表全量 ORDER BY Id 会打满 MySQL sort_buffer;组包仍在内存排序,并补充 (AreaId, Id) 索引脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-10-07 18:42:41 +08:00
parent 9e5d6d20d5
commit a9f8373ec3
2 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,18 @@
-- 分区明细按 AreaId 全量/分页查询时避免 filesort 打满 sort_buffer(查价 LIVE 路径易踩)
SET @db := DATABASE();
SET @idx := (
SELECT COUNT(1)
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @db
AND TABLE_NAME = 't_kpi_area_info'
AND INDEX_NAME = 'idx_kpi_info_area_id'
);
SET @sql := IF(
@idx = 0,
'ALTER TABLE `t_kpi_area_info` ADD KEY `idx_kpi_info_area_id` (`AreaId`, `Id`)',
'SELECT ''idx_kpi_info_area_id already exists'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

View File

@ -21,9 +21,10 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
public async Task<List<KpiAreaInfo>> GetInfosAsync(int areaId, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
{
var db = await GetDbContextAsync(cancellationToken);
// 依赖 idx_kpi_info_area_id (AreaId, Id),避免大分区 filesort
return await db.Queryable<KpiAreaInfo>()
.Where(x => x.AreaId == areaId)
.OrderBy(x => x.Id, SqlSugar.OrderByType.Desc)
.OrderBy(x => x.Id, OrderByType.Desc)
.ToPageListAsync(pageIndex, pageSize, cancellationToken);
}
@ -66,9 +67,10 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
public async Task<List<KpiAreaInfo>> GetAllInfosByAreaIdAsync(int areaId, CancellationToken cancellationToken = default)
{
var db = await GetDbContextAsync(cancellationToken);
// 大分区表(如美区邮编)全量 ORDER BY 会打满 MySQL sort_buffer 导致 500;
// 需要稳定顺序的调用方在内存中排序(如 EnsurePackAsync)。
return await db.Queryable<KpiAreaInfo>()
.Where(x => x.AreaId == areaId)
.OrderBy(x => x.Id)
.ToListAsync(cancellationToken);
}