Karen Tsirunyan
Karen Tsirunyan

Reputation: 1998

Is the insertion optimal?

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

Upvotes: 5

Views: 151

Answers (1)

Neel
Neel

Reputation: 21315

If you see the log then you can check that

2011-11-22 10:15:37,188 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2011-11-22 10:15:37,188 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,191 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'
2011-11-22 10:15:37,191 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,193 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2011-11-22 10:15:37,193 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,194 INFO sqlalchemy.engine.base.Engine SHOW COLLATION
2011-11-22 10:15:37,194 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,200 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2011-11-22 10:15:37,201 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,203 INFO sqlalchemy.engine.base.Engine DESCRIBE `users`
2011-11-22 10:15:37,203 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,204 INFO sqlalchemy.engine.base.Engine ROLLBACK
2011-11-22 10:15:37,206 INFO sqlalchemy.engine.base.Engine 
CREATE TABLE users (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    firstname VARCHAR(40), 
    lastname INTEGER, 
    PRIMARY KEY (id)
)


2011-11-22 10:15:37,206 INFO sqlalchemy.engine.base.Engine ()
2011-11-22 10:15:37,331 INFO sqlalchemy.engine.base.Engine COMMIT
2011-11-22 10:15:37,333 INFO sqlalchemy.engine.base.Engine INSERT INTO users (id, firstname, lastname) VALUES (%s, %s, %s)
2011-11-22 10:15:37,333 INFO sqlalchemy.engine.base.Engine ((1, 'Name1', 'Lname1'), (2, 'Name2', 'Lname2'), (3, 'Name3', 'Lname3'))

Only one insert with all values so I think this is the best way to insert bulk/batch data.

Upvotes: 3

Related Questions