Created
May 22, 2016 04:41
-
-
Save ericsalim/c8df8adeb9ec49fa8cb1b5c5e8d811f8 to your computer and use it in GitHub Desktop.
Select 100k rows using SQLAlchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
import sys | |
import time | |
conn = None | |
class Generic: | |
pass | |
try: | |
conn_str = "postgresql+psycopg2://postgres:password@localhost:5432/test" | |
engine = create_engine(conn_str) | |
conn = engine.connect() | |
sql = "select a,b,c,d,e from table1" | |
start = time.time() | |
res = conn.execute(sql).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