update queries to use pool, new indices, and materialized views for performance gain

This commit is contained in:
2026-04-09 16:11:51 -04:00
parent 087a765a2b
commit b39670d59c
2 changed files with 26 additions and 27 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ all_conus_landfalls <- get_all_conus_landfalls()
all_lf_type_storms <- get_all_lf_type_landfalls() all_lf_type_storms <- get_all_lf_type_landfalls()
onStop(function() { onStop(function() {
disconnect_db() poolClose(con)
}) })
storm_selection <- reactiveValues( storm_selection <- reactiveValues(
+25 -26
View File
@@ -4,12 +4,13 @@ library(tidyverse)
library(dplyr) library(dplyr)
library(DBI) library(DBI)
library(xts) library(xts)
library(pool)
config <- config::get(file = "config.yml") config <- config::get(file = "config.yml")
# SUPABASE CON # SUPABASE CON
con <- dbConnect( con <- dbPool(
RPostgres::Postgres(), RPostgres::Postgres(),
host = config$db_host, host = config$db_host,
port = config$db_port, port = config$db_port,
@@ -18,6 +19,14 @@ con <- dbConnect(
password = config$db_password 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 %>% #qry <- econ.storm_base_loss %>%
# left_join(view.hurdat_track, by = c("storm_basin", "storm_year", "storm_name")) # left_join(view.hurdat_track, by = c("storm_basin", "storm_year", "storm_name"))
@@ -41,11 +50,11 @@ con <- dbConnect(
# lf_id = 1 # lf_id = 1
#) #)
agnes <- list( #agnes <- list(
storm_basin = "AL", # storm_basin = "AL",
storm_year = 1972, # storm_year = 1972,
storm_name = "AGNES" # storm_name = "AGNES"
) #)
# helper functions # helper functions
@@ -73,14 +82,14 @@ get_yearly_economics <- function(yr) {
return(result) return(result)
} }
get_latest_normalization_year <- function() { #get_latest_normalization_year <- function() {
query <- tbl(con, "all_yearly_normalized_losses") %>% # query <- tbl(con, "all_yearly_normalized_losses") %>%
summarize(latest_year = max(normalization_year)) # summarize(latest_year = max(normalization_year))
#
result <- query %>% collect() # result <- query %>% collect()
#
return(result) # return(result)
} #}
get_storm_data_coverage <- function() { get_storm_data_coverage <- function() {
query <- tbl(con, "storm_data_coverage") query <- tbl(con, "storm_data_coverage")
@@ -366,11 +375,6 @@ get_normalized_metric_growth <- function(storm, full_lf_id) {
tbl(con, I("public.counties")), tbl(con, I("public.counties")),
by = c("state_fips" = "statefp", "county_fips" = "countyfp") by = c("state_fips" = "statefp", "county_fips" = "countyfp")
) %>% ) %>%
mutate(
geom_wkt = sql(
"ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(geom, 4326), .001))"
)
) %>%
select( select(
state_fips, state_fips,
county_fips, county_fips,
@@ -416,7 +420,7 @@ get_normalized_metric_growth <- function(storm, full_lf_id) {
state_fips state_fips
) )
baseline_metrics <- tbl(con, "yearly_state_metrics") %>% baseline_metrics <- tbl(con, I("metrics.yearly_state_metrics")) %>%
filter(year == storm$storm_year) %>% filter(year == storm$storm_year) %>%
inner_join(affected_state, by = "state_fips") %>% inner_join(affected_state, by = "state_fips") %>%
select( select(
@@ -425,7 +429,7 @@ get_normalized_metric_growth <- function(storm, full_lf_id) {
baseline_housing = state_housing_units baseline_housing = state_housing_units
) )
normalized_metrics <- tbl(con, "yearly_state_metrics") %>% normalized_metrics <- tbl(con, I("metrics.yearly_state_metrics")) %>%
filter( filter(
year >= storm$storm_year year >= storm$storm_year
) %>% ) %>%
@@ -452,11 +456,6 @@ get_normalized_metric_growth <- function(storm, full_lf_id) {
rename(state_fips = statefp), rename(state_fips = statefp),
by = "state_fips" by = "state_fips"
) %>% ) %>%
mutate(
geom_wkt = sql(
"ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(wkb_geometry, 4326), .001))"
)
) %>%
select( select(
state_fips, state_fips,
year, year,