31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sqlalchemyのinsert高速化

Last updated at Posted at 2017-09-28

記事の概要

結論

  • sqlalchemy.core速い。最も遅いやり方の55倍くらい

実験

実験結果

手法 所要時間(sec)
1.最も遅いやり方 1.486
2.ORMでまとめてInsert 0.369
3.sqlalchemy.coreでまとめてInsert 0.027

対象DBのモデル

# models.py抜粋

class Item(Base):
    __tablename__ = 'items'
    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    age = Column(Integer(), nullable=False)

1.最も遅いやり方

for i in range(0, 1000):
    item = Item()
    item.name = 'tarou'
    item.age = randint(1, 100000)
    session.add(item)
    session.commit()

2.ORMでまとめてInsert

items = list()
for i in range(0, 1000):
    item = Item()
    item.name = 'tarou'
    item.age = randint(1, 100000)
    items.append(item)
session.add_all(items)
session.commit()

3.sqlalchemy.coreでまとめてInsert

items = list()
for i in range(0, 1000):
    item = dict()
    item['name'] = 'bean'
    item['age'] = randint(1, 100000)
    items.append(item)
session.execute(Item.__table__.insert(), items)
session.commit()

31
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?