我使用python进行编码,使用psql保存数据。我的问题是,当我写入数据库时,大约需要2-3分钟。数据大小约为1,200,000 (行)和3列。
插入函数:
def store_data(cur,table_name,data):
cur.executemany(
"INSERT INTO"+" "+table_name+" "+"(name, date,id) VALUES (%s, %s, %s)",
[(data[i][0], data[i][1], data[i][2]) for i in xrange(0,len(data))]
)
cur.connection.commit()如何对函数进行加速?
发布于 2017-09-02 21:30:52
使用COPY命令。Postgres Documentation。也可以在副本中查看心理医生documentation。
一些数字:300万行的单独插入:3小时。使用复制:7秒。
发布于 2017-09-02 23:46:13
卓越 PostgreSQL文档中有一个关于"Populating a Database"的详细章节
除了像W.Mann建议的那样使用COPY之外,如果您有更多的性能要求,还可以做更多的事情:
归档和流分析删除索引temporary
如果使用pg_restore,可以尝试在多处理器系统上使用-j选项并行运行多个作业。并查看上面链接的文档中给出的其他选项。
发布于 2017-09-03 01:10:34
查看executemany的文档
Warning
In its current implementation this method is not faster than
executing execute() in a loop. For better performance you can use
the functions described in Fast execution helpers. 在同一位置可以找到到:http://initd.org/psycopg/docs/extras.html#fast-exec的链接,他们推荐:
psycopg2.extras.execute_batchhttps://stackoverflow.com/questions/46014168
复制相似问题