Skip to content

Commit

Permalink
chore: improve instruments view performance
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisgsmith committed Jan 4, 2025
1 parent dfe6082 commit 14d6e6b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
10 changes: 5 additions & 5 deletions api/internal/db/instrument.sql_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions api/internal/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions api/migrations/repeat/0040__views_instruments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE OR REPLACE VIEW v_instrument_telemetry AS (
LEFT JOIN telemetry_iridium ti ON a.telemetry_id = ti.id
);

CREATE OR REPLACE VIEW v_instrument AS (
CREATE OR REPLACE VIEW v_instrument_raw AS (
SELECT
i.id,
s.status_id,
Expand All @@ -22,7 +22,8 @@ CREATE OR REPLACE VIEW v_instrument AS (
i.show_cwms_tab,
t.name "type",
t.icon,
ST_AsGeoJSON(i.geometry)::json geometry,
i.geometry,
i.geometry_3857,
i.station,
i.station_offset "offset",
i.created_by,
Expand Down Expand Up @@ -129,7 +130,7 @@ CREATE OR REPLACE VIEW v_instrument AS (
WHERE NOT i.deleted
);

CREATE OR REPLACE VIEW v_instrument_geojson AS (
CREATE OR REPLACE VIEW v_instrument AS (
SELECT
id,
status_id,
Expand All @@ -139,9 +140,9 @@ CREATE OR REPLACE VIEW v_instrument_geojson AS (
name,
type_id,
show_cwms_tab,
name "type",
"type",
icon,
ST_GeomFromGeoJSON(geometry) "geometry",
ST_AsGeoJSON("geometry") "geometry",
station,
"offset",
created_by,
Expand All @@ -157,7 +158,7 @@ CREATE OR REPLACE VIEW v_instrument_geojson AS (
groups,
alert_configs,
opts
FROM v_instrument
FROM v_instrument_raw
);

CREATE OR REPLACE VIEW v_instrument_group AS (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE instrument ADD COLUMN geometry_3857 geometry(Point, 3857) GENERATED ALWAYS AS
(ST_Transform("geometry", 3857)) STORED;
8 changes: 4 additions & 4 deletions api/queries/instrument.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ with bbox as (
mvtgeom as (
select
row_number() over(order by id) as fid,
ST_AsMVTGeom(ST_Transform(t.geometry, 3857), bbox.tile_env::box2d, 4096, 256, false) as geometry,
ST_AsMVTGeom(t.geometry_3857, bbox.tile_env::box2d, 4096, 256, false) as geometry,
t.*,
ST_AsGeoJSON(t.geometry) as geometry
from v_instrument_geojson t, bbox
where ST_Intersects(ST_Transform(t.geometry, 3857), bbox.tile_env)
from v_instrument_raw t, bbox
where ST_Intersects(t.geometry_3857, bbox.tile_env)
)
select ST_AsMVT(mvtgeom.*)::bytea from mvtgeom;

Expand All @@ -58,7 +58,7 @@ select json_build_object(
'features', coalesce(json_agg(ST_AsGeoJSON(features.*)::json), '[]')
)
from (
select t.* from v_instrument_geojson t, bounds
select t.* from v_instrument_raw t, bounds
where ((sqlc.narg(project_id)::uuid is null) or (sqlc.narg(project_id)::uuid = any(
select project_id from project_instrument where instrument_id = t.id
)))
Expand Down

0 comments on commit 14d6e6b

Please sign in to comment.