内联大型数据集最佳实践

时间:2019-10-12 00:39:04

标签: r parallel-processing dplyr bigdata

我正在尝试使用dplyr::inner_join合并两个大型数据集(每行约350万行)。 我正在使用40核以上的强大计算机。我不确定我是否会利用机器本身,因为我无论如何都不会并行化任务。 我该如何解决这个需要花很多时间才能解决的问题?

最佳

2 个答案:

答案 0 :(得分:1)

我不认为3.5M内部联接会出现性能问题,除非由于数据集中关键列的重复(联接列的重复值),两个最终数据集将在联接之后3.5M * 3.5M

通常在R中,没有可以利用多个内核的函数。为此,您必须将可分别处理的数据分成批处理,然后将最终结果组合在一起并进一步计算。这是使用库dplyrdoParallel

的伪代码
library(dplyr)
library(doParallel)

# Parallel configuration #####
cpuCount <- 10
# Note that doParallel will replicated your environment to and process on multiple core
# so if your environment is 10GB memory & you use 10 core
# it would required 10GBx10=100GB RAM to process data parallel
registerDoParallel(cpuCount)

data_1 # 3.5M rows records with key column is id_1 & value column value_1
data_2 # 3.5M rows records with key columns are id_1 & id_2

# Goal is to calculate some stats/summary of value_1 for each combination of id_1 + id_2
id_1_unique <- unique(data_1$id_1)
batchStep <- 1000
batch_id_1 <- seq(1, length(id_1_unique )+batchStep , by=batchStep )

# Do the join for each batch id_1 & summary/calculation then return the final_data
# foreach will result a list, for this psuedo code it is a list of datasets
# which can be combined use bind_rows
summaryData <- bind_rows(foreach(index=1:(length(batch_id_1)-1)) %dopar% {
    batch_id_1_current <- id_1_unique[index:index+batchStep-1]
    batch_data_1 <- data_1 %>% filter(id_1 %in% batch_id_1_current)
    joined_data <- inner_join(batch_data_1, batch_data_2, by="id_1")
    final_data <- joined_data %>%
        group_by(id_1, id_2) %>%
        #calculation code here
        summary(calculated_value_1=sum(value_1)) %>%
        ungroup()
    return(final_data)
})


答案 1 :(得分:0)

您应该尝试使用data.table软件包,对于大型数据集,该软件包比dplyr多more efficient。我从here.

复制了内部联接代码
library(data.table)
DT <- data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
X  <- data.table(x=c("c","b"), v=8:7, foo=c(4,2))
DT[X, on="x", nomatch=0] # inner join
                         # SELECT DT INNER JOIN X ON DT$x = X$x

尽管data.table不使用并行化,但是它比inner_join更快,并且是我所知的最佳选择。