-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add materialized view to improve the display of meshes on map #519
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# coding: utf-8 | ||
from geoalchemy2.types import Geometry | ||
from sqlalchemy import Column, Integer, MetaData, Table, Text | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
from atlas.utils import engine | ||
|
||
metadata = MetaData() | ||
Base = declarative_base() | ||
|
||
|
||
class TMaillesTerritoire(Base): | ||
__table__ = Table( | ||
"t_mailles_territoire", | ||
metadata, | ||
Column("id_maille", Integer, primary_key=True, unique=True), | ||
Column("the_geom", Geometry()), | ||
Column("geojson_maille", Text), | ||
schema="atlas", | ||
autoload=True, | ||
autoload_with=engine, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
DROP TABLE IF EXISTS atlas.t_mailles_territoire; | ||
|
||
-- MV for having only meshs of the territory | ||
CREATE TABLE atlas.t_mailles_territoire | ||
AS SELECT | ||
st_transform(c.geom, 4326) AS the_geom, | ||
st_asgeojson(st_transform(c.geom, 4326)) AS geojson_maille, | ||
c.id_area AS id_maille | ||
FROM ref_geo.l_areas c | ||
JOIN ref_geo.bib_areas_types t ON t.id_type = c.id_type | ||
JOIN atlas.t_layer_territoire mt ON ST_intersects(c.geom,st_transform(mt.the_geom, find_srid('ref_geo', 'l_areas', 'geom'))) | ||
WHERE c.enable = true AND t.type_code = :type_maille; | ||
CREATE TABLE atlas.t_mailles_territoire AS | ||
SELECT | ||
st_transform(a.geom, 4326) AS the_geom, | ||
st_asgeojson(st_transform(a.geom, 4326)) AS geojson_maille, | ||
a.id_area AS id_maille | ||
FROM ref_geo.l_areas AS a | ||
JOIN ref_geo.bib_areas_types AS t | ||
ON t.id_type = a.id_type | ||
JOIN atlas.t_layer_territoire AS l | ||
ON ST_intersects(a.geom, st_transform(l.the_geom, find_srid('ref_geo', 'l_areas', 'geom'))) | ||
WHERE a.enable = true | ||
AND t.type_code = :type_maille ; | ||
|
||
CREATE UNIQUE INDEX t_mailles_territoire_id_maille_idx ON atlas.t_mailles_territoire USING btree (id_maille); | ||
CREATE UNIQUE INDEX ON atlas.t_mailles_territoire | ||
USING btree (id_maille); | ||
CREATE INDEX ON atlas.t_mailles_territoire | ||
USING spgist (the_geom); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
CREATE MATERIALIZED VIEW atlas.vm_observations_mailles | ||
AS SELECT obs.cd_ref, | ||
obs.id_observation, | ||
m.id_maille, | ||
m.geojson_maille, | ||
date_part('year', dateobs) as annee | ||
FROM atlas.vm_observations obs | ||
JOIN atlas.t_mailles_territoire m ON st_intersects(obs.the_geom_point, m.the_geom) | ||
CREATE MATERIALIZED VIEW atlas.vm_observations_mailles AS | ||
SELECT | ||
o.cd_ref, | ||
date_part('year', o.dateobs) AS annee, | ||
m.id_maille, | ||
COUNT(o.id_observation) AS nbr | ||
FROM atlas.vm_observations AS o | ||
JOIN atlas.t_mailles_territoire AS m | ||
ON (o.the_geom_point && m.the_geom) | ||
GROUP BY o.cd_ref, date_part('year', o.dateobs), m.id_maille | ||
ORDER BY o.cd_ref, annee | ||
WITH DATA; | ||
|
||
create unique index on atlas.vm_observations_mailles (id_observation); | ||
create index on atlas.vm_observations_mailles (id_maille); | ||
create index on atlas.vm_observations_mailles (cd_ref); | ||
CREATE UNIQUE INDEX ON atlas.vm_observations_mailles | ||
USING btree (cd_ref, annee, id_maille); | ||
|
||
CREATE INDEX ON atlas.vm_observations_mailles | ||
USING btree (annee); | ||
|
||
CREATE INDEX ON atlas.vm_observations_mailles | ||
USING btree (id_maille, cd_ref); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
DROP MATERIALIZED VIEW IF EXISTS atlas.vm_observations_mailles; | ||
DROP TABLE IF EXISTS atlas.t_mailles_territoire; | ||
|
||
|
||
CREATE TABLE atlas.t_mailles_territoire AS | ||
SELECT | ||
st_transform(a.geom, 4326) AS the_geom, | ||
st_asgeojson(st_transform(a.geom, 4326)) AS geojson_maille, | ||
a.id_area AS id_maille | ||
FROM ref_geo.l_areas AS a | ||
JOIN ref_geo.bib_areas_types AS t | ||
ON t.id_type = a.id_type | ||
JOIN atlas.t_layer_territoire AS l | ||
ON ST_intersects(a.geom, st_transform(l.the_geom, find_srid('ref_geo', 'l_areas', 'geom'))) | ||
WHERE a.enable = true | ||
AND t.type_code = :type_maille ; | ||
|
||
CREATE UNIQUE INDEX ON atlas.t_mailles_territoire | ||
USING btree (id_maille); | ||
|
||
CREATE INDEX ON atlas.t_mailles_territoire | ||
USING spgist (the_geom); | ||
|
||
|
||
CREATE MATERIALIZED VIEW atlas.vm_observations_mailles AS | ||
SELECT | ||
o.cd_ref, | ||
date_part('year', o.dateobs) AS annee, | ||
m.id_maille, | ||
COUNT(o.id_observation) AS nbr | ||
FROM atlas.vm_observations AS o | ||
JOIN atlas.t_mailles_territoire AS m | ||
ON (o.the_geom_point && m.the_geom) | ||
GROUP BY o.cd_ref, date_part('year', o.dateobs), m.id_maille | ||
ORDER BY o.cd_ref, annee | ||
WITH DATA; | ||
|
||
CREATE UNIQUE INDEX ON atlas.vm_observations_mailles | ||
USING btree (cd_ref, annee, id_maille); | ||
|
||
CREATE INDEX ON atlas.vm_observations_mailles | ||
USING btree (annee); | ||
|
||
CREATE INDEX ON atlas.vm_observations_mailles | ||
USING btree (id_maille, cd_ref); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Est-ce que le
order by
sert pour la VM ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je n'ai pas vérifié... Mais le fait d'ordonner les enregistrements par
cd_ref
etannee
fait que la requête qui agrège les données pour générer la carte à potentiellement moins de "pages" (au sens stockage par Postgresql) à consulter pour les récupérer. Cela peut donc participer à la rapidité d’exécution de la requête.En outre, les index comme BRIN nécessite d'ordonner les lignes. Je n'ai pas tester ce type d'index ici mais c'est possiblement plus rapide et pourrait être une solution intéressante à tester...
Enfin, malgré le
ORDER BY
la requête s'exécute très rapidement même avec 28 millions de données. Donc, ce n'est pas un facteur limitant.