使用 SQLAlchemy ORM 批量插入

2021-11-20 00:00:00 python sqlalchemy database orm mysql

有什么方法可以让 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.

相关文章