I want to insert some data into a table and I want the multiple insertion to be as fast as possible. I use sqlalchemy library for Python. I want to know if my insertion is optimal, or there is a better way to do that.Here is my code:
from sqlalchemy import *
from sqlalchemy import schema
metadata = schema.MetaData()
engine = create_engine('sqlite:///:memory:', echo=True)
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('firstname', String(40)),
Column('lastname', Integer),
)
metadata.create_all(engine)
ins = users.insert()
conn = engine.connect()
conn.execute(users.insert(), [
{'id': 1, 'firstname' : 'Name1', 'lastname' : 'Lname1'},
{'id': 2, 'firstname' : 'Name2', 'lastname' : 'Lname2'},
{'id': 3, 'firstname' : 'Name3', 'lastname' : 'Lname3'}
])
metadata.bind = engine
If you see the log then you can check that
Only one insert with all values so I think this is the best way to insert bulk/batch data.