首页 > 解决方案 > 从 R 从 MySQL 加载大数据

问题描述

我在 MySQL 中有一个大表(大约 300 万行,15 列),我正在尝试在 R 闪亮应用程序中使用表中的一些数据。

我已经能够获得连接并在 R 中编写查询:

library(DBI)
library(dplyr)
cn <- dbConnect(drv      = RMySQL::MySQL(), 
                username = "user", 
                password = "my_password",
                host     = "host", 
                port     = 3306
                )


query = "SELECT * FROM dbo.locations"

然而,当我运行dbGetQuery(cn, query)它需要很长时间(我最终关闭了我的 RStudio 程序,因为它变得无响应。

我也试过

res <- DBI::dbSendQuery (cn, query)
repeat {
  df <- DBI::dbFetch (res, n = 1000)
  if (nrow (df) == 0) { break }
}
dbClearResult(dbListResults(cn)[[1]])

因为这类似于按块读取数据,但由于df某种原因,我的结果有 0 行。

关于如何在 R 中获取我的表的任何建议?我什至应该尝试将该表读入 R 吗?据我了解,R 不能很好地处理大数据。

标签: mysqlr

解决方案


推荐阅读