且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL查询在工作台上工作良好,但在R中花费的时间太长

更新时间:2023-11-30 19:27:34

解决方案可能是将dbGetQuery替换为dbSendQuerydbFetch调用.

The solution could be to replace dbGetQuery with dbSendQuery and dbFetch call.

简单的步骤可能是:

library(RMySQL)

# From OP
con <- dbConnect(RMySQL::MySQL(),
               dbname ="mydb",
               host      = "localhost",
               port  = 3306,
               user  = "root",
               password = "")

# iterationresults is a table in your database. One can replace query with his own
rs = dbSendQuery(con, "select * from iterationresults")

# Fetch first 20 rows and repeat it for all rows  
df = dbFetch(rs, n=20)

# For repeated call
while (!dbHasCompleted(rs)){
 df<- dbFetch(rs, n=20)
}
# OR Fetch all rows in one go
df = dbFetch(rs, n=-1)

# Free all resources
dbClearResult(rs) 
# Close connection
dbDisconnect(con)
# df will contain results i.e.
df
#   ID Truck_ID Speed trip_id
#1  11  TTI 039     6     217
#2  12  TTI 039     6     217
# ........