【问题标题】:How to Insert Huge Pandas Dataframe in MySQL table with Parallel Insert Statement?如何使用并行插入语句在 MySQL 表中插入巨大的 Pandas 数据框?
【发布时间】:2019-05-31 04:21:31
【问题描述】:

我正在做一个项目,我必须编写一个包含数百万行和大约 25 列的数据框,其中大部分是数字类型。我正在使用Pandas DataFrame to SQL Function 将数据帧转储到 Mysql 表中。我发现这个函数创建了一个可以一次插入多行的 Insert 语句。这是一个很好的方法,但是 MySQL 对使用这种方法可以构建的查询长度有限制。

有没有办法在同一个表中并行插入,这样我就可以加快进程?

【问题讨论】:

  • 在类似的问题here中有一些有趣的建议

标签: mysql pandas pandasql


【解决方案1】:

您可以做一些事情来实现这一目标。

一种方法是在写入 sql 时使用附加参数。

df.to_sql(method = 'multi')

根据这个documentation,将'multi'传递给方法参数允许您批量插入。

另一种解决方案是使用 multiprocessing.dummy 构建自定义插入函数。 这是文档的链接:https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.dummy

import math
from multiprocessing.dummy import Pool as ThreadPool

...

def insert_df(df, *args, **kwargs):
    nworkers = 4 # number of workers that executes insert in parallel fashion

    chunk = math.floor(df.shape[0] / nworkers) # number of chunks
    chunks = [(chunk * i, (chunk * i) + chunk) for i in range(nworkers)]
    chunks.append((chunk * nworkers, df.shape[0]))
    pool = ThreadPool(nworkers)

    def worker(chunk):
        i, j = chunk
        df.iloc[i:j, :].to_sql(*args, **kwargs)

    pool.map(worker, chunks)
    pool.close()
    pool.join()

....

insert_df(df, "foo_bar", engine, if_exists='append')

https://stackoverflow.com/a/42164138/5614132提出了第二种方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2012-12-13
    • 1970-01-01
    • 2010-12-11
    • 2016-10-04
    相关资源
    最近更新 更多