From 217c066f3e1808afabdba73c0b1508a8a8ca4dee Mon Sep 17 00:00:00 2001 From: Hidde Wieringa Date: Sat, 16 Nov 2024 19:08:04 +0100 Subject: [PATCH] Optimize `signals` table and related materialized views (#146) --- import/docker-startup.sh | 4 ++-- import/openrailwaymap.lua | 2 ++ import/sql/signals_with_azimuth.sql.js | 8 ++++++-- import/sql/tile_views.sql | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/import/docker-startup.sh b/import/docker-startup.sh index c0eb5fa6..2758d225 100755 --- a/import/docker-startup.sh +++ b/import/docker-startup.sh @@ -131,5 +131,5 @@ $PSQL --tuples-only -c "with bounds as (SELECT st_transform(st_setsrid(ST_Extent echo "Import bounds: $(cat /data/import/bounds.json)" echo "Database summary" -$PSQL -c "select table_name as table, pg_size_pretty(pg_total_relation_size(quote_ident(table_name))) as size from information_schema.tables where table_schema = 'public' order by table_name;" -$PSQL -c "select pg_size_pretty(sum(pg_total_relation_size(quote_ident(table_name)))) as total_size from information_schema.tables where table_schema = 'public';" +$PSQL -c "select concat(relname, ' (', relkind ,')') as name, pg_size_pretty(pg_table_size(oid)) as size from pg_class where relkind in ('m', 'r', 'i') and relname not like 'pg_%' order by pg_table_size(oid) desc;" +$PSQL -c "select pg_size_pretty(SUM(pg_table_size(oid))) as size from pg_class where relkind in ('m', 'r', 'i') and relname not like 'pg_%';" diff --git a/import/openrailwaymap.lua b/import/openrailwaymap.lua index 93c421e5..e94aef0a 100644 --- a/import/openrailwaymap.lua +++ b/import/openrailwaymap.lua @@ -228,6 +228,8 @@ local signals = osm2pgsql.define_table({ name = 'signals', ids = { type = 'node', id_column = 'osm_id' }, columns = signal_columns, + -- The queried table is signals_with_azimuth + cluster = 'no', }) local boxes = osm2pgsql.define_table({ diff --git a/import/sql/signals_with_azimuth.sql.js b/import/sql/signals_with_azimuth.sql.js index ddff3377..9b7f52a3 100644 --- a/import/sql/signals_with_azimuth.sql.js +++ b/import/sql/signals_with_azimuth.sql.js @@ -73,14 +73,18 @@ CREATE OR REPLACE VIEW signals_with_azimuth_view AS ) as sl ON true WHERE (railway IN ('signal', 'buffer_stop') AND signal_direction IS NOT NULL) - OR railway IN ('derail', 'vacancy_detection'); + OR railway IN ('derail', 'vacancy_detection'); -- Use the view directly such that the query in the view can be updated CREATE MATERIALIZED VIEW IF NOT EXISTS signals_with_azimuth AS SELECT * FROM - signals_with_azimuth_view; + signals_with_azimuth_view + WHERE + signal_feature IS NOT NULL + OR speed_feature IS NOT NULL + OR electrification_feature IS NOT NULL; CREATE INDEX IF NOT EXISTS signals_with_azimuth_geom_index ON signals_with_azimuth diff --git a/import/sql/tile_views.sql b/import/sql/tile_views.sql index 6e0264ad..03acf1ba 100644 --- a/import/sql/tile_views.sql +++ b/import/sql/tile_views.sql @@ -281,7 +281,7 @@ CREATE OR REPLACE VIEW speed_railway_signals AS speed_feature_type as type, azimuth, (signal_direction = 'both') as direction_both - FROM signals_with_azimuth s + FROM signals_with_azimuth WHERE railway = 'signal' AND speed_feature IS NOT NULL ORDER BY