0
    sect<-c("Healthcare","Basic Materials","Utilities","Financial Services","Technology","Consumer" 
    "Defensive","Industrials","Communication Services","Energy","Real Estate","Consumer 
    Cyclical","NULL")

    mcap<-c("3 - Large","2 - Mid","1 - Small")

    df_total = data.frame()
    start <- as.Date("01-01-14",format="%d-%m-%y")
    end   <- as.Date("18-03-20",format="%d-%m-%y")
    theDate <- start

    while (theDate <= end){
      for (value1 in sect){
        for (value2 in mcap){
            date=theDate
            sector<-value1
            marketcap1<-value2
            newquery("Select * from table where date='%s' and sector='%s' and marketcap='%s'",date,sector,marketcap1)
   topdemo <- sqlQuery(dbhandle,newquery)
   df=data.frame(topdemo)
   df_total <- rbind(df_total,df)

     }
    }
   theDate <- theDate + 1 
   }

在我的程序中,我做了一些 SQL 计算,而不是“选择”查询。我需要这段代码从 2014 年运行到 2020 年,但执行它需要很多时间。有什么办法可以减少执行时间?该数据库为每个市值和行业提供了许多股票价格。

4

1 回答 1

1

运行一个查询而不是所有循环:

select *
from table
where sector in ('Healthcare', 'Basic Materials', 'Utilities',
                 'Financial Services', 'Technology', 'Consumer' 
                 'Defensive', 'Industrials', 'Communication Services', 'Energy', 'Real Estate', 'Consumer Cyclical', 'NULL'
                 ) and
        marketcap in ('3 - Large', '2 - Mid', '1 - Small') and
        date between '2014-01-01 and '2020-03-18';

运行大量小查询会产生很多开销,通常一个更好。

也就是说,您似乎正在移动大量数据。我想知道是否有必要进行所有数据移动。

奇怪的是,您正在循环数以千计的日期,但不包括查询中的日期。

于 2020-03-19T10:48:34.657 回答