<i id='4gkTL'><tr id='4gkTL'><dt id='4gkTL'><q id='4gkTL'><span id='4gkTL'><b id='4gkTL'><form id='4gkTL'><ins id='4gkTL'></ins><ul id='4gkTL'></ul><sub id='4gkTL'></sub></form><legend id='4gkTL'></legend><bdo id='4gkTL'><pre id='4gkTL'><center id='4gkTL'></center></pre></bdo></b><th id='4gkTL'></th></span></q></dt></tr></i><div id='4gkTL'><tfoot id='4gkTL'></tfoot><dl id='4gkTL'><fieldset id='4gkTL'></fieldset></dl></div>
<tfoot id='4gkTL'></tfoot>

      <bdo id='4gkTL'></bdo><ul id='4gkTL'></ul>

    1. <small id='4gkTL'></small><noframes id='4gkTL'>

      <legend id='4gkTL'><style id='4gkTL'><dir id='4gkTL'><q id='4gkTL'></q></dir></style></legend>

      使用 SQLAlchemy ORM 批量插入

      时间:2023-06-03
        <bdo id='nqxzS'></bdo><ul id='nqxzS'></ul>
        <tfoot id='nqxzS'></tfoot>
          <legend id='nqxzS'><style id='nqxzS'><dir id='nqxzS'><q id='nqxzS'></q></dir></style></legend>

              • <small id='nqxzS'></small><noframes id='nqxzS'>

                <i id='nqxzS'><tr id='nqxzS'><dt id='nqxzS'><q id='nqxzS'><span id='nqxzS'><b id='nqxzS'><form id='nqxzS'><ins id='nqxzS'></ins><ul id='nqxzS'></ul><sub id='nqxzS'></sub></form><legend id='nqxzS'></legend><bdo id='nqxzS'><pre id='nqxzS'><center id='nqxzS'></center></pre></bdo></b><th id='nqxzS'></th></span></q></dt></tr></i><div id='nqxzS'><tfoot id='nqxzS'></tfoot><dl id='nqxzS'><fieldset id='nqxzS'></fieldset></dl></div>
                  <tbody id='nqxzS'></tbody>

              • 本文介绍了使用 SQLAlchemy ORM 批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有什么方法可以让 SQLAlchemy 进行批量插入而不是插入每个单独的对象.即,

                Is there any way to get SQLAlchemy to do a bulk insert rather than inserting each individual object. i.e.,

                正在做:

                INSERT INTO `foo` (`bar`) VALUES (1), (2), (3)
                

                而不是:

                INSERT INTO `foo` (`bar`) VALUES (1)
                INSERT INTO `foo` (`bar`) VALUES (2)
                INSERT INTO `foo` (`bar`) VALUES (3)
                

                我刚刚将一些代码转换为使用 sqlalchemy 而不是原始 sql,虽然现在使用它更好了,但现在似乎变慢了(最多 10 倍),我想知道这是否是原因.

                I've just converted some code to use sqlalchemy rather than raw sql and although it is now much nicer to work with it seems to be slower now (up to a factor of 10), I'm wondering if this is the reason.

                也许我可以更有效地使用会话来改善这种情况.目前我有 autoCommit=False 并在添加一些东西后执行 session.commit() .尽管如果在其他地方更改数据库,这似乎会导致数据过时,例如即使我执行新查询,我仍然会得到旧结果?

                May be I could improve the situation using sessions more efficiently. At the moment I have autoCommit=False and do a session.commit() after I've added some stuff. Although this seems to cause the data to go stale if the DB is changed elsewhere, like even if I do a new query I still get old results back?

                感谢您的帮助!

                推荐答案

                SQLAlchemy在1.0.0版本中介绍:

                SQLAlchemy introduced that in version 1.0.0:

                批量操作 - SQLAlchemy 文档

                通过这些操作,您现在可以进行批量插入或更新!

                With these operations, you can now do bulk inserts or updates!

                例如,您可以:

                s = Session()
                objects = [
                    User(name="u1"),
                    User(name="u2"),
                    User(name="u3")
                ]
                s.bulk_save_objects(objects)
                s.commit()
                

                在这里,将进行批量插入.

                Here, a bulk insert will be made.

                这篇关于使用 SQLAlchemy ORM 批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从 MySQL 表中删除唯一约束 下一篇:如何获取 MySQL 中所有表的所有列的名称?

                相关文章

              • <tfoot id='0tTxZ'></tfoot>

                <small id='0tTxZ'></small><noframes id='0tTxZ'>

                  <i id='0tTxZ'><tr id='0tTxZ'><dt id='0tTxZ'><q id='0tTxZ'><span id='0tTxZ'><b id='0tTxZ'><form id='0tTxZ'><ins id='0tTxZ'></ins><ul id='0tTxZ'></ul><sub id='0tTxZ'></sub></form><legend id='0tTxZ'></legend><bdo id='0tTxZ'><pre id='0tTxZ'><center id='0tTxZ'></center></pre></bdo></b><th id='0tTxZ'></th></span></q></dt></tr></i><div id='0tTxZ'><tfoot id='0tTxZ'></tfoot><dl id='0tTxZ'><fieldset id='0tTxZ'></fieldset></dl></div>

                    <bdo id='0tTxZ'></bdo><ul id='0tTxZ'></ul>
                    <legend id='0tTxZ'><style id='0tTxZ'><dir id='0tTxZ'><q id='0tTxZ'></q></dir></style></legend>