6

I am trying to read a small table from SQL and I'm looking into switching over to SQLAlchemy from pyodbc to be able to use pd.to_sql()

When I compare the two, the sql alchemy is much slower.

s_py = """\
import pandas as pd
import pyodbc
cxn = pyodbc.connect('DRIVER={SQL SERVER};SERVER=.\;DATABASE=PPIS;UID=sa;PWD=pwd')
"""

s_alch = """\
import pandas as pd
import sqlalchemy
cxn = sqlalchemy.create_engine("mssql+pyodbc://sa:pwd@./PPIS?driver=SQL+Server")
"""
timeit.timeit('pd.read_sql("SELECT * FROM Operators", cxn)',setup=s_py, number=100)

Out[21]: 0.18496091418973037

timeit.timeit('pd.read_sql("SELECT * FROM Operators", cxn)',setup=s_alch, number=100)

Out[23]: 4.407356934717654

I've seen this that talks about inserting rows. But why would the read be so much slower?

Faller
  • 1,588
  • 3
  • 16
  • 27
  • Have you tried using SQL Profiler and/or ODBC traces to see if your two methods produce significantly different results? – Gord Thompson Feb 16 '18 at 14:24

0 Answers0