Оптимизировать код R (чтение и запись SQL) для более быстрой работы - PullRequest
0 голосов
/ 29 октября 2019

Нижеследующий двойной вложенный цикл работает в 850 филиалах, 2 точках обслуживания и 25 специальных потребностях. Общее время работы составляет 70 часов. Пожалуйста, помогите мне оптимизировать, чтобы он работал быстрее?

Код начинается здесь:

    start_time <- Sys.time()

     for(i in levels(branch_code)) #850 branches 
    {
     for(j in levels(service_point)) #2 service points
    {
    for(u in levels(specific_need)) #25 specific needs
    {
      test<-paste0("SELECT * from dbo.Data_Base where calendar_date <= '2018-11-30' and branch_code = '",i, "'  and service_point = '",j, "' and specific_need = '",u, "' order by calendar_date asc")
      testtwo<-sqlQuery(con,test)

      if(nrow(testtwo)>500)     
      {

        df<-testtwo[,c("volume","calendar_date")]
        colnames(df)<-c("y","ds")

        m <-prophet(weekly.seasonality = TRUE, yearly.seasonality = TRUE,  daily.seasonality = FALSE, holidays=holidays)
        m<-add_seasonality(m, name='monthly',period=30.4,fourier.order = 8,prior.scale = 13)
        m<-fit.prophet(m,df)

        future <- make_future_dataframe(m, periods = 100, freq = "day", include_history = TRUE)
        forecast <- predict(m, future)   

        writeto<-data.frame(forecast$ds,i,j,u,forecast$yhat)
        colnames(writeto)<-c("ds","branch_code","service_point","specific_need","Forecast") 
        writeto_two<-left_join(writeto, df,by = ('ds'))

        colnames(writeto_two)<-c("calendar_date","branch_code","service_point","specific_need","Forecast","Volume")   

        testtwowrite<-data.frame(writeto_two)
        dbWriteTable(connec, "dbo.Actual_Volume_write", testtwowrite, append = TRUE)
      }
      else 
      {
        print(c(i,j,u))
      }
    }
  }
}
end_time <- Sys.time()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...