mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
making db migrations to improve performance
This commit is contained in:
parent
50b11a8df3
commit
047d40df86
9 changed files with 15063 additions and 53 deletions
|
|
@ -77,58 +77,35 @@ export async function GET(
|
|||
// `);
|
||||
|
||||
const result = await db.execute(sql`
|
||||
WITH latest_plans AS (
|
||||
SELECT DISTINCT ON (property_id)
|
||||
*
|
||||
FROM plan
|
||||
WHERE portfolio_id = ${pid}
|
||||
AND scenario_id = ${sid}
|
||||
ORDER BY property_id, created_at DESC
|
||||
),
|
||||
|
||||
recommendation_flags AS (
|
||||
SELECT
|
||||
r.id AS recommendation_id,
|
||||
r.measure_type AS measure_type,
|
||||
r.property_id AS property_id,
|
||||
r.estimated_cost AS estimated_cost,
|
||||
r.type AS type
|
||||
|
||||
FROM latest_plans 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
|
||||
|
||||
WHERE r.default = true
|
||||
AND r.already_installed = false
|
||||
|
||||
GROUP BY
|
||||
r.id,
|
||||
r.measure_type,
|
||||
r.type,
|
||||
r.property_id,
|
||||
r.estimated_cost
|
||||
)
|
||||
|
||||
SELECT
|
||||
measure_type,
|
||||
type,
|
||||
|
||||
COUNT(DISTINCT property_id)::int AS homes_count,
|
||||
SUM(estimated_cost)::float AS total_cost,
|
||||
AVG(estimated_cost)::float AS average_cost
|
||||
|
||||
FROM recommendation_flags
|
||||
r.measure_type,
|
||||
r.type,
|
||||
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
|
||||
WHERE r.default = true
|
||||
AND r.already_installed = false
|
||||
GROUP BY
|
||||
measure_type,
|
||||
type
|
||||
r.measure_type,
|
||||
r.type
|
||||
ORDER BY total_cost DESC;
|
||||
`);
|
||||
|
||||
|
|
|
|||
1
src/app/db/migrations/0142_serious_whirlwind.sql
Normal file
1
src/app/db/migrations/0142_serious_whirlwind.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
CREATE INDEX "idx_recommendation_active_defaults" ON "recommendation" USING btree ("id") WHERE "recommendation"."default" = true AND "recommendation"."already_installed" = false;
|
||||
1
src/app/db/migrations/0143_magenta_magus.sql
Normal file
1
src/app/db/migrations/0143_magenta_magus.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
CREATE INDEX "idx_plan_latest_per_property" ON "plan" USING btree ("portfolio_id","scenario_id","property_id","created_at" DESC NULLS LAST);
|
||||
2
src/app/db/migrations/0144_lovely_moira_mactaggert.sql
Normal file
2
src/app/db/migrations/0144_lovely_moira_mactaggert.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
CREATE INDEX CONCURRENTLY "idx_plan_recommendations_plan_rec" ON "plan_recommendations" USING btree ("plan_id","recommendation_id");--> statement-breakpoint
|
||||
CREATE INDEX CONCURRENTLY "idx_recommendation_active_id_property" ON "recommendation" USING btree ("id","property_id") WHERE "recommendation"."default" = true AND "recommendation"."already_installed" = false;
|
||||
4958
src/app/db/migrations/meta/0142_snapshot.json
Normal file
4958
src/app/db/migrations/meta/0142_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
4991
src/app/db/migrations/meta/0143_snapshot.json
Normal file
4991
src/app/db/migrations/meta/0143_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
5034
src/app/db/migrations/meta/0144_snapshot.json
Normal file
5034
src/app/db/migrations/meta/0144_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -995,6 +995,27 @@
|
|||
"when": 1767619224711,
|
||||
"tag": "0141_amazing_harry_osborn",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 142,
|
||||
"version": "7",
|
||||
"when": 1767692283901,
|
||||
"tag": "0142_serious_whirlwind",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 143,
|
||||
"version": "7",
|
||||
"when": 1767693565375,
|
||||
"tag": "0143_magenta_magus",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 144,
|
||||
"version": "7",
|
||||
"when": 1767704869539,
|
||||
"tag": "0144_lovely_moira_mactaggert",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ import {
|
|||
index,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { Material, material } from "./materials";
|
||||
import { InferModel } from "drizzle-orm";
|
||||
import { InferModel, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
||||
export const recommendation = pgTable(
|
||||
|
|
@ -46,7 +46,20 @@ export const recommendation = pgTable(
|
|||
labourDays: real("labour_days"),
|
||||
alreadyInstalled: boolean("already_installed").default(false),
|
||||
},
|
||||
(table) => [index("recommendation_property_id_idx").on(table.propertyId)]
|
||||
(table) => [
|
||||
index("recommendation_property_id_idx").on(table.propertyId),
|
||||
index("idx_recommendation_active_defaults")
|
||||
.on(table.id)
|
||||
.where(
|
||||
sql`${table.default} = true AND ${table.alreadyInstalled} = false`
|
||||
),
|
||||
|
||||
index("idx_recommendation_active_id_property")
|
||||
.on(table.id, table.propertyId)
|
||||
.where(
|
||||
sql`${table.default} = true AND ${table.alreadyInstalled} = false`
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
export const unitQuantity: [string, ...string[]] = ["m2", "part", "kwp"];
|
||||
|
|
@ -165,6 +178,12 @@ export const plan = pgTable(
|
|||
table.portfolioId,
|
||||
table.scenarioId
|
||||
),
|
||||
index("idx_plan_latest_per_property").on(
|
||||
table.portfolioId,
|
||||
table.scenarioId,
|
||||
table.propertyId,
|
||||
table.createdAt.desc()
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
|
|
@ -181,7 +200,13 @@ export const planRecommendations = pgTable(
|
|||
.notNull()
|
||||
.references(() => recommendation.id),
|
||||
},
|
||||
(table) => [index("idx_plan_recommendations_plan_id").on(table.planId)]
|
||||
(table) => [
|
||||
index("idx_plan_recommendations_plan_id").on(table.planId),
|
||||
index("idx_plan_recommendations_plan_rec").on(
|
||||
table.planId,
|
||||
table.recommendationId
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
export const HousingType: [string, ...string[]] = ["Private", "Social"];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue