我正在尝试从SQL读取一个小表,我正在考虑从pyodbc切换到SQLAlchemy,以便能够使用pd.to_sql()
当我比较两者时,sql炼金术要慢得多。
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)
出[21]:0.18496091418973037
timeit.timeit('pd.read_sql("SELECT * FROM Operators", cxn)',setup=s_alch, number=100)
出[23]:4.407356934717654
我见过关于插入行的this。但为什么读取会慢得多?