Created
March 4, 2019 11:08
-
-
Save pplonski/5671d1ff0335db0479e2352d8a556774 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import numpy as np | |
import pandas as pd | |
from sqlalchemy import create_engine | |
import psycopg2 | |
import time | |
host = "host-address-here" | |
user = "db-user-here" | |
password = "db-password" | |
db = "db-name" | |
alchemy_engine = "postgresql://{}:{}@{}:5432/{}".format(user, password, host, db) | |
pg_engine = "user='{}' password='{}' host='{}' dbname='{}'".format(user, password, host, db) | |
sql_queries = ["select * from projects_project", | |
"select count(*) from projects_project"] | |
for sql_query in sql_queries: | |
print(sql_query) | |
for lib_type in ["alchemy", "pg"]: | |
t = [] | |
for i in range(10): | |
start = time.time() | |
if lib_type == "alchemy": | |
engine = create_engine(alchemy_engine) | |
df = pd.read_sql_query(sql_query, engine) | |
elif lib_type == "pg": | |
conn = psycopg2.connect(pg_engine) | |
cur = conn.cursor() | |
cur.execute(sql_query) | |
df = pd.DataFrame(cur.fetchall()) | |
end = time.time() | |
t += [end-start] | |
print(lib_type, ",", sql_query, ",", np.mean(t)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The results:
The projects_project has ~500 rows in the table.
The blog post: https://pplonski.github.io/sqlalchemy-vs-psycopg2/