perf: 物化视图加速门店详情和健康分API - 29秒降到毫秒级

This commit is contained in:
freedakgmail
2026-07-30 09:49:40 +08:00
parent e7cadef6bd
commit d1ea57872a
3 changed files with 11 additions and 11 deletions
+4 -4
View File
@@ -733,7 +733,7 @@ router.get('/store-overview', async (req: AuthRequest, res) => {
count(*) FILTER (WHERE variance_level = '绿色-基本正常') AS green_count,
count(*) FILTER (WHERE variance_level = '灰色-口径异常') AS gray_count,
round(sum(food_cost_variance)::numeric, 2) AS total_variance
FROM analytics.v_store_theoretical_actual_cost_april
FROM analytics.mv_store_theoretical_actual_cost_april
`)
sendSuccess(res, result.rows[0])
} catch (err: any) {
@@ -750,7 +750,7 @@ router.get('/store-map', async (req: AuthRequest, res) => {
round(c.theoretical_cost_rate_pct::numeric, 2) AS theo_cost_rate,
round(c.actual_food_cost_rate_pct::numeric, 2) AS actual_cost_rate,
l.latitude_gcj02, l.longitude_gcj02
FROM analytics.v_store_theoretical_actual_cost_april c
FROM analytics.mv_store_theoretical_actual_cost_april c
LEFT JOIN analytics.v_store_location_operating l ON l.store_code = c.store_code
WHERE l.latitude_gcj02 IS NOT NULL
`)
@@ -777,7 +777,7 @@ router.get('/store-ranking', async (req: AuthRequest, res) => {
const validSorts = ['food_cost_variance', 'theoretical_cost_rate_pct', 'actual_food_cost_rate_pct', 'variance_to_theoretical_pct', 'negative_item_lines', 'store_name']
const sortCol = validSorts.includes(sort) ? sort : 'food_cost_variance'
const countResult = await query(`SELECT count(*) FROM analytics.v_store_theoretical_actual_cost_april ${where}`)
const countResult = await query(`SELECT count(*) FROM analytics.mv_store_theoretical_actual_cost_april ${where}`)
const total = countResult.rows[0].count
const result = await query(`
@@ -788,7 +788,7 @@ router.get('/store-ranking', async (req: AuthRequest, res) => {
round(food_cost_variance::numeric, 2) AS variance_amount,
negative_item_lines,
variance_level
FROM analytics.v_store_theoretical_actual_cost_april
FROM analytics.mv_store_theoretical_actual_cost_april
${where}
ORDER BY ${sortCol} ${order} NULLS LAST LIMIT $1 OFFSET $2
`, [pageSize, offset])