首页 > 解决方案 > 加速 pandas.DataFrame.to_sql (PostgreSQL)

问题描述

我正在将一个DataFrame形状(500K, 10)写入我的本地 postgres 数据库。目前,写入大约需要 60 秒。我想加快速度。我做了一些研究,发现使用method = 'multi'chunksize参数to_sql帮助人们大大减少了写入时间。但我不是这种情况。我的写入时间几乎保持不变或几乎没有减少。

此外,我还尝试使用fast_executemany(下面的片段)

engine = create_engine('postgresql://postgres:Opex123@localhost:5432/Annual_Business_Plan_Baseline_8_155')
schema = 'public'

####
from sqlalchemy import event

@event.listens_for(engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
    if executemany:
        cursor.fast_executemany = True
        cursor.commit()
####

但它给出了以下错误 - psycopg2.extensions.cursor' object has no attribute 'fast_executemany'

任何成功度过这一挑战的人?

标签: pythonpandaspostgresql

解决方案


推荐阅读