很慢的原因:主要是varchar转为clob存入,数据长度太长
通过sqlalchemy中的types将所有的varchar类型固定为varchar格式
from sqlalchemy import create_engine,types
engine = create_engine('oracle://biu:biubiu23@192.333.300.33:1521/stock')
#设置写入类型,不然默认是用CLOB类型写入,内置的类型转换很慢,小量数据无所谓
dtyp = {c:types.VARCHAR(df[c].str.len().max()) for c in df.columns[df.dtypes == 'object'].tolist()} # 转换类型
#不设置to_sql的方法,设置写入类型
df.to_sql(t_name, con=engine, if_exists='append', index=False, index_label=None, dtype=dtyp) # 插入,真的很好用
#关闭引擎
engine.dispose()
参考:
https://blog.csdn.net/baidu_39148260/article/details/103341108