Skip to content

Instantly share code, notes, and snippets.

@thoo
Forked from jsheedy/pg_copy_from.py
Created March 7, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoo/d68087eafec591b29d222c3c1d7bc73a to your computer and use it in GitHub Desktop.
Save thoo/d68087eafec591b29d222c3c1d7bc73a to your computer and use it in GitHub Desktop.
benchmark for postgres inserts using copy_from and IteratorFile from https://gist.github.com/jsheedy/ed81cdf18190183b3b7d
import time
import psycopg2
from iter_file import IteratorFile
conn = psycopg2.connect(host="localhost", database="test")
# args = [(1,2), (3,4), (5,6)]
args = [(i,i+1) for i in range(1,1*10**4,2)]
def time_test(func):
def f():
truncate_table()
t1 = time.time()
func()
t2 = time.time()
print(str(t2 - t1) + ": " + func.__name__ )
return f
def truncate_table():
with conn.cursor() as cur:
cur.execute("truncate t");
conn.commit()
@time_test
def query_builder_insert():
with conn.cursor() as cur:
records_list_template = ','.join(['%s'] * len(args))
insert_query = 'insert into t (a, b) values {0}'.format(records_list_template)
cur.execute(insert_query, args)
conn.commit()
@time_test
def copy_from_insert():
with conn.cursor() as cur:
f = IteratorFile(("{}\t{}".format(x[0], x[1]) for x in args))
cur.copy_from(f, 't', columns=('a', 'b'))
conn.commit()
query_builder_insert()
copy_from_insert()
# original
0.008857011795043945: query_builder_insert
0.0029380321502685547: copy_from_insert
# 10 records
0.00867605209350586: query_builder_insert
0.003248929977416992: copy_from_insert
# 10k records
0.041108131408691406: query_builder_insert
0.010066032409667969: copy_from_insert
# 1M records
3.464181900024414: query_builder_insert
0.47070908546447754: copy_from_insert
# 10M records
38.96936798095703: query_builder_insert
5.955034017562866: copy_from_insert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment