I am looking to insert/update a large volume of data stored in either a list of tuples or list of dictionaries into an existing table using SQLAlchemy. What is the most efficient way to do this?
What I have right now is this,
session_maker = sqlalchemy.orm.sessionmaker()
session_maker .configure(bind=engine)
inspector = sqlalchemy.inspect(engine)
session = session_maker()
meta_data = sqlalchemy.MetaData(engine)
MyTable = sqlalchemy.Table('MyTable', meta_data, autoload=True, autoload_with=engine)
MyTable.insert().execute([{'key1': 'value1', 'key2': 'value2'}, {'key1': 'value3', 'key2': 'value4'}])
I have also heard of bulk_insert_mappings()
and bulk_save_objects()
. I would, however, like to avoid creating any other objects as explained here Bulk insert with SQLAlchemy ORM and use either a list of tuples or dictionaries as mentioned above.
How can I do bulk updates?