Cla*_*diu 6 python database postgresql performance bulkinsert
我正在使用python通过postgres将数千行的批量插入到表中:
def bulk_insert_copyfrom(cursor, table_name, field_names, values):
if not values: return
#print "bulk copy from prepare..."
str_vals = "\n".join("\t".join(adapt_to_str(val) for val in cur_vals) for cur_vals in values)
strf = StringIO(str_vals)
#print "bulk copy from execute..."
cursor.copy_from(strf, table_name, columns=tuple(field_names))
Run Code Online (Sandbox Code Playgroud)
插入16000行需要一段时间,所以我决定一次插入1000以查看会发生什么,并在插入过程中获得更细粒度的视图.在此表中插入1000行只需2-3秒,每行有14列.在我看来,这应该发生得更快.实际上,1000行中的一些比其他行更快.为什么这次手术没有花费更少的时间?我已经VACUUM ANALYZE定期跑步,这确实加速了它,但它仍然比我想要的慢.