【问题标题】:Suppress output of SQL statements when calling pandas to_sql()调用 pandas to_sql() 时抑制 SQL 语句的输出
【发布时间】:2021-03-11 18:58:19
【问题描述】:

to_sql() 正在打印我的 Jupyter Notebook 中的每个插入语句,这使得数百万条记录的运行速度非常缓慢。如何显着降低冗长?我还没有找到这个函数的任何详细设置。我试过%%capture 写在这里。同样的方法适用于另一个简单的测试用例print(),但不适用于to_sql()How do you suppress output in IPython Notebook?

from sqlalchemy import create_engine, NVARCHAR
import cx_Oracle

df.to_sql('table_name', engine, if_exists='append', 
              schema='schema', index=False, chunksize=10000,
                dtype={col_name: NVARCHAR(length=20) for col_name in df} )

【问题讨论】:

  • 您的连接 URI 是否包含 echo=True(或 echo=False 以外的任何内容)?
  • @GordThompson 是的,就是这样!你想发布答案还是我应该?或者这是一个微不足道的问题,值得删除?我在任何地方都找不到答案。现在我知道要查找什么了,很容易找到:docs.sqlalchemy.org/en/13/core/…
  • 你继续发布答案;它可以在未来帮助其他人。
  • 谢谢,@GordThompson,我刚刚做到了。仅供参考,现在我可以在 18 分钟内写出 2300 万条记录。在大约一个小时之前,但我并不确定,因为 Jupyter Notebook 已经完全陷入困境,即使写作已经完成,单元格也从未显示为已完成。
  • ... 实际上,请忽略我之前的评论:method="multi"。我刚刚针对 OracleXE 18.4 实例进行了测试,它显然不支持表值构造函数,这是 method="multi" 用来尝试加快速度的。也许 cx-Oracle DBAPI 层对.executemany() 做了自己的优化。

标签: pandas oracle sqlalchemy jupyter-notebook


【解决方案1】:

create_engine() 中,设置echo=False,所有日志记录都将被禁用。更多细节在这里:https://docs.sqlalchemy.org/en/13/core/engines.html#more-on-the-echo-flag

from sqlalchemy import create_engine, NVARCHAR
import cx_Oracle

host='hostname.net'
port=1521
sid='DB01' #instance is the same as SID
user='USER'
password='password'
sid = cx_Oracle.makedsn(host, port, sid=sid)

cstr = 'oracle://{user}:{password}@{sid}'.format(
    user=user,
    password=password,
    sid=sid
)

engine =  create_engine(
    cstr,
    convert_unicode=False,
    pool_recycle=10,
    pool_size=50,
    echo=False
)

感谢@GordThompson 为我指明了正确的方向!

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 2019-07-05
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多