Bulk insert with SQLAlchemy ORM

前端 未结 10 1115
刺人心
刺人心 2020-11-30 17:15

Is there any way to get SQLAlchemy to do a bulk insert rather than inserting each individual object. i.e.,

doing:

INSERT INTO `foo` (`bar`) VALUES (1         


        
10条回答
  •  悲哀的现实
    2020-11-30 17:53

    The best answer I found so far was in sqlalchemy documentation:

    http://docs.sqlalchemy.org/en/latest/faq/performance.html#i-m-inserting-400-000-rows-with-the-orm-and-it-s-really-slow

    There is a complete example of a benchmark of possible solutions.

    As shown in the documentation:

    bulk_save_objects is not the best solution but it performance are correct.

    The second best implementation in terms of readability I think was with the SQLAlchemy Core:

    def test_sqlalchemy_core(n=100000):
        init_sqlalchemy()
        t0 = time.time()
        engine.execute(
            Customer.__table__.insert(),
                [{"name": 'NAME ' + str(i)} for i in xrange(n)]
        )
    

    The context of this function is given in the documentation article.

提交回复
热议问题