mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
trying to ooptimise the measures table
This commit is contained in:
parent
047d40df86
commit
8b113ee106
2 changed files with 27 additions and 18 deletions
|
|
@ -83,26 +83,23 @@ export async function GET(
|
|||
COUNT(DISTINCT r.property_id)::int AS homes_count,
|
||||
SUM(r.estimated_cost)::float AS total_cost,
|
||||
AVG(r.estimated_cost)::float AS average_cost
|
||||
FROM (
|
||||
SELECT DISTINCT ON (p.property_id)
|
||||
p.id,
|
||||
p.property_id
|
||||
FROM plan p
|
||||
WHERE p.portfolio_id = ${pid}
|
||||
AND p.scenario_id = ${sid}
|
||||
ORDER BY p.property_id, p.created_at DESC
|
||||
) lp
|
||||
JOIN plan_recommendations pr
|
||||
ON pr.plan_id = lp.id
|
||||
JOIN recommendation r
|
||||
ON r.id = pr.recommendation_id
|
||||
LEFT JOIN recommendation_materials rm
|
||||
ON rm.recommendation_id = r.id
|
||||
LEFT JOIN material m
|
||||
ON m.id = rm.material_id
|
||||
AND m.is_active = true
|
||||
FROM recommendation r
|
||||
WHERE r.default = true
|
||||
AND r.already_installed = false
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM (
|
||||
SELECT DISTINCT ON (p.property_id)
|
||||
p.id
|
||||
FROM plan p
|
||||
WHERE p.portfolio_id = ${pid}
|
||||
AND p.scenario_id = ${sid}
|
||||
ORDER BY p.property_id, p.created_at DESC
|
||||
) lp
|
||||
JOIN plan_recommendations pr
|
||||
ON pr.plan_id = lp.id
|
||||
WHERE pr.recommendation_id = r.id
|
||||
)
|
||||
GROUP BY
|
||||
r.measure_type,
|
||||
r.type
|
||||
|
|
|
|||
12
src/app/db/migrations/2026_01_06_recommendation_cover.sql
Normal file
12
src/app/db/migrations/2026_01_06_recommendation_cover.sql
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-- migrations/2026_01_06_recommendation_cover.sql
|
||||
|
||||
CREATE INDEX CONCURRENTLY idx_recommendation_active_cover
|
||||
ON recommendation (
|
||||
id,
|
||||
property_id,
|
||||
measure_type,
|
||||
type
|
||||
)
|
||||
INCLUDE (estimated_cost)
|
||||
WHERE default = true
|
||||
AND already_installed = false;
|
||||
Loading…
Add table
Reference in a new issue