SQLAlchemy:flush() 和 commit() 有什么区别? [英] SQLAlchemy: What's the difference between flush() and commit()?

查看:77
本文介绍了SQLAlchemy:flush() 和 commit() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLAlchemy 中的 flush()commit() 有什么区别?

What the difference is between flush() and commit() in SQLAlchemy?

我已经阅读了文档,但并不明智 - 他们似乎假设了我没有的预先理​​解.

I've read the docs, but am none the wiser - they seem to assume a pre-understanding that I don't have.

我对它们对内存使用的影响特别感兴趣.我正在将一些数据从一系列文件(总共大约 500 万行)加载到数据库中,我的会话偶尔会失败 - 这是一个大型数据库和一台内存不多的机器.

I'm particularly interested in their impact on memory usage. I'm loading some data into a database from a series of files (around 5 million rows in total) and my session is occasionally falling over - it's a large database and a machine with not much memory.

我想知道我是否使用了太多的 commit() 而没有足够的 flush() 调用 - 但没有真正理解有什么区别,这很难告诉!

I'm wondering if I'm using too many commit() and not enough flush() calls - but without really understanding what the difference is, it's hard to tell!

推荐答案

Session 对象基本上是对数据库进行更改(更新、插入、删除)的持续事务.这些操作在提交之前不会持久化到数据库中(如果您的程序在会话中间事务中因某种原因中止,则其中的任何未提交的更改都将丢失).

A Session object is basically an ongoing transaction of changes to a database (update, insert, delete). These operations aren't persisted to the database until they are committed (if your program aborts for some reason in mid-session transaction, any uncommitted changes within are lost).

会话对象使用 session.add() 注册事务操作,但在调用 session.flush() 之前,还不会将它们传送到数据库.

The session object registers transaction operations with session.add(), but doesn't yet communicate them to the database until session.flush() is called.

session.flush() 向数据库传达一系列操作(插入、更新、删除).数据库将它们作为事务中的挂起操作进行维护.在数据库收到当前事务的 COMMIT(这就是 session.commit() 所做的)之前,更改不会永久保存到磁盘,也不会对其他事务可见.

session.flush() communicates a series of operations to the database (insert, update, delete). The database maintains them as pending operations in a transaction. The changes aren't persisted permanently to disk, or visible to other transactions until the database receives a COMMIT for the current transaction (which is what session.commit() does).

session.commit() 将这些更改提交(保留)到数据库.

session.commit() commits (persists) those changes to the database.

flush()总是作为对commit()(1).

当您使用 Session 对象查询数据库时,查询将返回来自数据库和它持有的未提交事务的刷新部分的结果.默认情况下,Session 对象 autoflush 他们的操作,但可以禁用.

When you use a Session object to query the database, the query will return results both from the database and from the flushed parts of the uncommitted transaction it holds. By default, Session objects autoflush their operations, but this can be disabled.

希望这个例子能让这个更清楚:

Hopefully this example will make this clearer:

#---
s = Session()

s.add(Foo('A')) # The Foo('A') object has been added to the session.
                # It has not been committed to the database yet,
                #   but is returned as part of a query.
print 1, s.query(Foo).all()
s.commit()

#---
s2 = Session()
s2.autoflush = False

s2.add(Foo('B'))
print 2, s2.query(Foo).all() # The Foo('B') object is *not* returned
                             #   as part of this query because it hasn't
                             #   been flushed yet.
s2.flush()                   # Now, Foo('B') is in the same state as
                             #   Foo('A') was above.
print 3, s2.query(Foo).all() 
s2.rollback()                # Foo('B') has not been committed, and rolling
                             #   back the session's transaction removes it
                             #   from the session.
print 4, s2.query(Foo).all()

#---
Output:
1 [<Foo('A')>]
2 [<Foo('A')>]
3 [<Foo('A')>, <Foo('B')>]
4 [<Foo('A')>]

这篇关于SQLAlchemy:flush() 和 commit() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆