python - 加速 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'
任何成功度过这一挑战的人?
解决方案
推荐阅读
- amazon-ses - 如何使用 SES “从”客户的电子邮件发送电子邮件
- go - 带有交互式提示的 golang 简单服务器
- postgresql - 如何将 Postgres CLI 中的转储数据库从远程服务器传输到远程服务器或本地计算机?
- flutter - 如何做流构建器以在颤动中从 bloc 获取数据
- javascript - 未按正确顺序调用 Javascript 函数,需要异步
- microsoft-edge - 我的边缘启动非常缓慢。如何解决?
- angular - 从 Typescript 中的动态字符串数组中获取值
- python-3.x - 如果python中不满足条件,如何转到函数的第一行?
- javascript - 如何为 Google 自定义搜索 API json 结果设置 HTML 元素的样式?
- javascript - 如何确保在调用函数之前履行承诺?