Home >Database >Mysql Tutorial >How can SQLAlchemy achieve optimized bulk insertions like a single SQL statement instead of individual insertions?
Question:
Can SQLAlchemy perform bulk insertions, similar to a single SQL statement (INSERT INTO foo (bar) VALUES (1), (2), (3)), instead of executing separate insertions for each object?
Answer:
SQLAlchemy version 1.0.0 introduced bulk operations, including bulk inserts and updates.
Bulk Inserts:
In version 1.0.0 and later, SQLAlchemy supports bulk inserts using the bulk_save_objects() method:
s = Session() objects = [ User(name="u1"), User(name="u2"), User(name="u3") ] s.bulk_save_objects(objects) s.commit()
This code will perform a single bulk insert operation to add all three User objects.
Improved Efficiency:
Utilizing bulk inserts significantly improves performance compared to executing separate insertions for each object.
Stale Data and Transactions:
To maintain data consistency, use sessions with autocommit=False. When you manually commit a transaction using session.commit(), SQLAlchemy flushes the modified objects and fetches the updated records during subsequent queries. However, database modifications from external sources may still cause stale data issues. To mitigate this, consider using database locks to enforce transactional consistency.
The above is the detailed content of How can SQLAlchemy achieve optimized bulk insertions like a single SQL statement instead of individual insertions?. For more information, please follow other related articles on the PHP Chinese website!