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
+25 -26
View File
@@ -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,