I read the sqlalchemy documentation, but some nuances escape me.

Is the code

session.add(item) session.commit() 

equivalent code

 session.merge(item) 

?

If not - what's the difference? If so, can I, instead of a large number of merge, use the same number of add, and then one general commit? Do I understand correctly that in this case one sql query will actually be executed instead of a set, and this will save execution time?

If so, after what number add should commit?

The fact is that I now support someone else's code. In it, tens of thousands of new items are created in cycles, which need to be added (or updated if lines already exist with the same primary key) into the database, and the script takes two to three hours. Can I expect to speed up after replacing merge with add + commit? Could this lead to unexpected problems?

Database on PostgreSQL, if that matters.

  • one
    A related question about a large number of objects : .stackoverflow.com / questions / 631587 / - m9_psy
  • one
    Regarding the question: is not, or is with a very big stretch - merge () does more than just add, update the object. Actually, the documentation says what exactly the method does. There is also an unreadable paragraph about why this merge is needed - and it is not in vain located in the Session State Management section, that is, not quite intended to add / update data in the database. - m9_psy

0