Skip to content

Commit

Permalink
use temp table to improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugurtha Bouhadoun committed Jul 5, 2024
1 parent 48b39c8 commit 0a36aa0
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 252 deletions.
7 changes: 7 additions & 0 deletions db-migrations/migrations/20240117135326-init-tables.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ module.exports = {

// Grant permissions to ban user
await queryInterface.sequelize.query(`GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA ban TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT USAGE ON SCHEMA ban TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT ALL PRIVILEGES ON SCHEMA ban TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ban TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT USAGE ON SCHEMA public TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT ALL PRIVILEGES ON SCHEMA public TO "${POSTGRES_BAN_USER}";`)
await queryInterface.sequelize.query(`GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "${POSTGRES_BAN_USER}";`)
} catch (error) {
console.error(error)
}
Expand Down

This file was deleted.

35 changes: 0 additions & 35 deletions db-migrations/migrations/20240618155829-init-address-view.cjs

This file was deleted.

93 changes: 62 additions & 31 deletions db-migrations/migrations/20240625132013-init-postal-code-view.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ const {POSTGRES_BAN_USER} = process.env
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
const bboxBufferAdressView = 50
const addressBboxBuffer = 200
const bboxBuffer = 100

try {
// Create the address_view

await queryInterface.sequelize.query(`
CREATE VIEW ban."address_view_cp" AS
WITH postal_codes_array AS (
WITH address_view AS (
SELECT
A.*,
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326)), 2154), ${bboxBufferAdressView}, 'join=mitre endcap=square'), 4326) AS bbox
FROM
ban.address AS A
WHERE A."isActive" = true
ORDER BY A.id ASC ),
postal_codes_array AS (
SELECT
a.*,
array_length(d."postalCodes", 1) AS array_length,
d."postalCodes" AS postalCodes,
b.meta->'insee'->>'cog' AS insee_com,
d."libelleAcheminementWithPostalCodes"
FROM
ban.address_view AS a
address_view AS a
LEFT JOIN
ban.district AS b
ON a."districtID" = b.id
Expand Down Expand Up @@ -69,18 +81,18 @@ module.exports = {
pca.postalCodes,
pca."libelleAcheminementWithPostalCodes",
CASE
WHEN pca.array_length = 1 THEN 'DATANOVA'
WHEN pca.array_length > 1 THEN
CASE
WHEN EXISTS (
SELECT 1
FROM external.postal_area AS c
WHERE pca.insee_com = c."inseeCom"
AND ST_Intersects(ST_Transform(pca.bbox, 2154), ST_Transform(c.geometry, 2154))
) THEN 'CONTOURS_CP'
ELSE 'DGFIP'
END
ELSE 'DGFIP'
WHEN pca.array_length = 1 THEN 'DATANOVA'
WHEN pca.array_length > 1 THEN
CASE
WHEN EXISTS (
SELECT 1
FROM external.postal_area AS c
WHERE pca.insee_com = c."inseeCom"
AND ST_Intersects(ST_Transform(pca.bbox, 2154), ST_Transform(c.geometry, 2154))
) THEN 'CONTOURS_CP'
ELSE 'DGFIP'
END
ELSE 'DGFIP'
END AS source_cp
FROM
postal_codes_array AS pca
Expand All @@ -90,7 +102,26 @@ module.exports = {

await queryInterface.sequelize.query(`
CREATE VIEW ban."common_toponym_view_cp" AS
WITH postal_codes_array AS (
WITH common_toponym_view AS(
SELECT
CT.id, CT."districtID", CT.labels, CT.geometry, CT."updateDate", CT.meta, CT.range_validity, CT."isActive",
ST_Centroid(ST_Collect(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326))) AS centroid,
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_Collect(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326))), 2154), ${addressBboxBuffer}, 'join=mitre endcap=square'), 4326) AS "addressBbox",
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_SetSRID(ST_GeomFromGeoJSON(CT.geometry), 4326)), 2154), ${bboxBuffer}, 'join=mitre endcap=square'), 4326) AS "bbox",
COUNT(A.id) AS "addressCount",
COUNT(DISTINCT CASE WHEN A.certified = true THEN A.id ELSE NULL END) AS "certifiedAddressCount"
FROM
ban.common_toponym AS CT
LEFT JOIN
ban.address AS A
ON
(CT.id = A."mainCommonToponymID"
OR CT.id = ANY(A."secondaryCommonToponymIDs")) AND A."isActive" = true
WHERE CT."isActive" = true
GROUP BY CT.id
ORDER BY CT.id ASC ),
postal_codes_array AS (
SELECT
ct.*,
b.meta->'insee'->>'cog' AS insee_com,
Expand All @@ -102,7 +133,7 @@ module.exports = {
ELSE ct."addressBbox"
END AS used_bbox
FROM
ban.common_toponym_view AS ct
common_toponym_view AS ct
LEFT JOIN
ban.district AS b
ON ct."districtID" = b.id
Expand Down Expand Up @@ -144,27 +175,27 @@ module.exports = {
SELECT pca."libelleAcheminementWithPostalCodes"->>c."postalCode"
FROM external.postal_area AS c
WHERE pca.insee_com = c."inseeCom"
ORDER BY ST_Area(ST_Intersection(ST_Transform(pca.used_bbox, 2154), ST_Transform(c.geometry, 2154))) DESC
ORDER BY ST_Area(ST_Intersection(ST_Transform(pca.bbox, 2154), ST_Transform(c.geometry, 2154))) DESC
LIMIT 1
)
ELSE NULL
END AS "libelleAcheminement",
pca.postalCodes,
pca."libelleAcheminementWithPostalCodes",
CASE
WHEN pca.array_length = 1 THEN 'DATANOVA'
WHEN pca.array_length > 1 THEN
CASE
WHEN EXISTS (
SELECT 1
FROM external.postal_area AS c
WHERE pca.insee_com = c."inseeCom"
AND ST_Intersects(ST_Transform(pca.used_bbox, 2154), ST_Transform(c.geometry, 2154))
) THEN 'CONTOURS_CP'
ELSE 'DGFIP'
END
ELSE 'DGFIP'
END AS source_cp,
CASE
WHEN pca.array_length = 1 THEN 'DATANOVA'
WHEN pca.array_length > 1 THEN
CASE
WHEN EXISTS (
SELECT 1
FROM external.postal_area AS c
WHERE pca.insee_com = c."inseeCom"
AND ST_Intersects(ST_Transform(pca.used_bbox, 2154), ST_Transform(c.geometry, 2154))
) THEN 'CONTOURS_CP'
ELSE 'DGFIP'
END
ELSE 'DGFIP'
END AS source_cp,
pca.used_bbox
FROM
postal_codes_array AS pca
Expand Down
Loading

0 comments on commit 0a36aa0

Please sign in to comment.