简体   繁体   English

何时提交多个插入/更新的Python DB-Api

[英]Python DB-Api when to commit on multiple inserts/updates

I have a function which updates a single row in db. 我有一个功能,它更新数据库中的一行。

def update_one_row(conn, condition, value):
    with conn.cursor() as curr:  
        curr.execute("""UPDATE persons p 
                        SET p.age=%s
                        WHERE p.name=%s;""",
                     (value, condition))

Is it ok to use this function multiple (couple thousand) times and do conn.commit() afterwards, like this: 可以多次使用此函数(几千次conn.commit()然后再执行conn.commit() ,如下所示:

from pymysql import Connect
connect_args = {...}
conn = Connect(**connect_args)
for condition, value in iterable_of_conditions_values:
    update_one_row(conn, condition, value)
# Here I visually inspect in jupyter notebook if things went as expected and I accidentaly did not screw up
conn.commit()

Or should I pass curr instead of conn to update_one_row ? 或者我应该通过curr代替connupdate_one_row

I am aware of curr.executemany() , but i prefer explicit loop. 我知道curr.executemany() ,但我更喜欢显式循环。 Is there a performance difference? 有性能差异吗?

Overall I am quite lost on the usage of cursors and when to commit. 总的来说,我对游标的使用以及何时提交完全迷失了。

You have to commit when you want to apply a bunch of changes and want to have the ability to rollback / not commiting if you encourter a problem somewhere. 当您要进行大量更改时必须提交,并且如果您在某个地方遇到问题,则必须具有回滚/不提交的功能。 In the most common case, this is used when changes only makes sense if applied together. 在最常见的情况下,只有将更改一起应用才有意义,才使用此选项。

In your case, that would makes sense to commit after a few thousands. 在您的情况下,数千次之后提交是有意义的。 To not overcomplicate your system, the best way would be to commit only one time after your loop. 为了不使系统过于复杂,最好的方法是在循环后仅提交一次。 Else, you would have to keep track of which rows where updated or not. 否则,您将必须跟踪哪些行已更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM