From b39670d59caf633adb053e5777ce1972f285b533 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Thu, 9 Apr 2026 16:11:51 -0400 Subject: [PATCH] update queries to use pool, new indices, and materialized views for performance gain --- app/global.R | 2 +- app/queries.R | 51 +++++++++++++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/app/global.R b/app/global.R index c4af04f..03970cd 100644 --- a/app/global.R +++ b/app/global.R @@ -47,7 +47,7 @@ all_conus_landfalls <- get_all_conus_landfalls() all_lf_type_storms <- get_all_lf_type_landfalls() onStop(function() { - disconnect_db() + poolClose(con) }) storm_selection <- reactiveValues( diff --git a/app/queries.R b/app/queries.R index 3a43b4b..be3283e 100644 --- a/app/queries.R +++ b/app/queries.R @@ -4,12 +4,13 @@ library(tidyverse) library(dplyr) library(DBI) library(xts) +library(pool) config <- config::get(file = "config.yml") # SUPABASE CON -con <- dbConnect( +con <- dbPool( RPostgres::Postgres(), host = config$db_host, port = config$db_port, @@ -18,6 +19,14 @@ con <- dbConnect( password = config$db_password ) +#con <- dbConnect( +# RPostgres::Postgres(), +# host = config$db_host, +# port = config$db_port, +# dbname = config$db_dbname, +# user = config$db_user, +# password = config$db_password +#) #qry <- econ.storm_base_loss %>% # left_join(view.hurdat_track, by = c("storm_basin", "storm_year", "storm_name")) @@ -41,11 +50,11 @@ con <- dbConnect( # lf_id = 1 #) -agnes <- list( - storm_basin = "AL", - storm_year = 1972, - storm_name = "AGNES" -) +#agnes <- list( +# storm_basin = "AL", +# storm_year = 1972, +# storm_name = "AGNES" +#) # helper functions @@ -73,14 +82,14 @@ get_yearly_economics <- function(yr) { return(result) } -get_latest_normalization_year <- function() { - query <- tbl(con, "all_yearly_normalized_losses") %>% - summarize(latest_year = max(normalization_year)) - - result <- query %>% collect() - - return(result) -} +#get_latest_normalization_year <- function() { +# query <- tbl(con, "all_yearly_normalized_losses") %>% +# summarize(latest_year = max(normalization_year)) +# +# result <- query %>% collect() +# +# return(result) +#} get_storm_data_coverage <- function() { query <- tbl(con, "storm_data_coverage") @@ -366,11 +375,6 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { tbl(con, I("public.counties")), by = c("state_fips" = "statefp", "county_fips" = "countyfp") ) %>% - mutate( - geom_wkt = sql( - "ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(geom, 4326), .001))" - ) - ) %>% select( state_fips, county_fips, @@ -416,7 +420,7 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { state_fips ) - baseline_metrics <- tbl(con, "yearly_state_metrics") %>% + baseline_metrics <- tbl(con, I("metrics.yearly_state_metrics")) %>% filter(year == storm$storm_year) %>% inner_join(affected_state, by = "state_fips") %>% select( @@ -425,7 +429,7 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { baseline_housing = state_housing_units ) - normalized_metrics <- tbl(con, "yearly_state_metrics") %>% + normalized_metrics <- tbl(con, I("metrics.yearly_state_metrics")) %>% filter( year >= storm$storm_year ) %>% @@ -452,11 +456,6 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { rename(state_fips = statefp), by = "state_fips" ) %>% - mutate( - geom_wkt = sql( - "ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(wkb_geometry, 4326), .001))" - ) - ) %>% select( state_fips, year,