Skip to content

Instantly share code, notes, and snippets.

@ericsalim
Created May 22, 2016 04:43
Show Gist options
  • Save ericsalim/f45d0f0d4babf0e59708c75804eb78c4 to your computer and use it in GitHub Desktop.
Save ericsalim/f45d0f0d4babf0e59708c75804eb78c4 to your computer and use it in GitHub Desktop.
Select 100k rows using Psycopg2
import psycopg2
import psycopg2.extras
import sys
import time
conn = None
class Generic:
pass
try:
conn = psycopg2.connect("host='localhost' dbname='test' user='postgres' password='password'")
cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
sql = "select a,b,c,d,e from table1"
start = time.time()
cur.execute(sql)
res = cur.fetchall()
elapsed = time.time() - start
print('query time: ' + str(elapsed * 1000))
start = time.time()
rowlist = []
for r in res:
g = Generic()
g.a = r['a']
g.b = r['b']
g.c = r['c']
g.d = r['d']
g.e = r['e']
rowlist.append(g)
elapsed = time.time() - start
print('conversion time: ' + str(elapsed * 1000))
print('res size: ' + str(sys.getsizeof(res)))
print('rowlist size: ' + str(sys.getsizeof(rowlist)))
except Exception, err:
print('error')
print(err)
finally:
if conn is not None: conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment