<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 批量插入

      Bulk insert with SQLAlchemy ORM(使用 SQLAlchemy ORM 批量插入)
        <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 批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Bogus foreign key constraint fail(虚假外键约束失败)
                how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
              • <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>

                            <tbody id='0tTxZ'></tbody>