perf(数据库): 优化数据库查询性能

- 在设备、机柜路由中强制使用 JOIN 避免 N+1 查询问题
- 添加连接池配置提升并发处理能力
- 设置 distinct 和 subQuery 参数确保查询准确性
This commit is contained in:
zhang1106
2026-02-10 14:35:36 +08:00
parent 18c3a3eb15
commit b9d89bee11
3 changed files with 35 additions and 15 deletions
+6 -3
View File
@@ -98,7 +98,7 @@ router.get('/', validateQuery(queryDeviceSchema), async (req, res) => {
where.rackId = rackId;
}
// 执行查询
// 执行查询 - 优化:使用 JOIN 避免 N+1 查询问题
const { count, rows } = await Device.findAndCountAll({
where,
include: [
@@ -106,11 +106,14 @@ router.get('/', validateQuery(queryDeviceSchema), async (req, res) => {
model: Rack,
include: [
{ model: Room }
]
],
separate: false // 强制使用 JOIN 而不是单独查询
}
],
offset,
limit: parseInt(pageSize)
limit: parseInt(pageSize),
distinct: true, // 避免 count 不准确
subQuery: false // 避免子查询导致的性能问题
});
res.json({