Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to get minimum data from Database

Tags:

r

postgresql

I have a Postgres database. I want to find the minimum value of a column called calendarid, which is of type integer and the format yyyymmdd, from a certain table. I am able to do so via the following code.

get_history_startdate <- function(src) {
  get_required_table(src) %>% # This gives me the table tbl(src, "table_name")
    select(calendarid) %>%
    as_data_frame %>%
    collect() %>%
    min() # Result : 20150131
}

But this method is really slow as it loads all the data from the database to the memory. Any ideas how can I improve it?

like image 652
mrtyormaa Avatar asked May 09 '26 15:05

mrtyormaa


1 Answers

get_required_table(src) %>% 
  summarise(max(calendarid, na.rm = TRUE)) %>% 
  pull

will run the appropriate SQL query.

like image 110
Scarabee Avatar answered May 12 '26 05:05

Scarabee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!