简体   繁体   English

python sqlalchemy bulk_save_objects 不使用批量

[英]python sqlalchemy bulk_save_objects doesn't use bulk

In continue to my previous post I'm trying to use the bulk_save_objects for a list of objects (the objects dont have a PK value therefore it should create it for each object).在继续我的一篇文章中,我尝试将 bulk_save_objects 用于对象列表(对象没有 PK 值,因此它应该为每个对象创建它)。 When I use the bulk_save_objects I see an insert per object instead of one insert for all objects.当我使用 bulk_save_objects 时,我看到每个对象都有一个插入,而不是所有对象的一个​​插入。

The code :编码 :

class Product(Base):

    __tablename__ = 'products'
    id = Column('id',BIGINT, primary_key=True)
    barcode = Column('barcode' ,BIGINT)
    productName = Column('name', TEXT,nullable=False)
    objectHash=Column('objectHash',TEXT,unique=True,nullable=False)

    def __init__(self, productData,picture=None):
        self.barcode = productData[ProductTagsEnum.barcode.value]
        self.productName = productData[ProductTagsEnum.productName.value]
        self.objectHash = md5((str(self.barcode)+self.produtName).encode('utf-8')).hexdigest()

Another class contains the following save method :另一个类包含以下保存方法:

def saveNewProducts(self,products):
    Session = sessionmaker()
    session=Session()
    productsHashes=[ product.objectHash for product in products]
    query = session.query(Product.objectHash).filter(Product.objectHash.in_(productsHashes))
    existedHashes=query.all()
    newProducts = [ product for product in products if product.objectHash not in productsHashes]
    /*also tried : session.bulk_save_objects(newProducts, preserve_order=False)*/

    session.bulk_save_objects(newProducts)

UPDATE 1更新 1

I following what @Ilja Everilä recommended in the comments, I added a few parameters to the connection string :我遵循@Ilja Everilä 在评论中推荐的内容,在连接字符串中添加了一些参数:

 engine = create_engine('postgresql://postgres:123@localhost:5432/mydb', pool_size=25, max_overflow=0,
                           executemany_mode='values',
                           executemany_values_page_size=10000, executemany_batch_page_size=500,
                           echo=True)

In the console I saw multiple inserts with the following format :在控制台中,我看到了以下格式的多个插入:

2019-09-16 16:48:46,509 INFO sqlalchemy.engine.base.Engine INSERT INTO products (barcode, productName, objectHash) VALUES (%(barcode)s, %(productName)s, %(objectHash)s, ) RETURNING products.id
2019-09-16 16:48:46,509 INFO sqlalchemy.engine.base.Engine {'barcode': '5008251', 'productName': 'ice ream','object_hash': 'b2752233ec523f2e874dc95b70020ae5'}

In my case, the solution I used : I deleted the id column and set the objectHash as PK, and afterwards the save_bulk and add_all functions worked and actually did bulk insert.就我而言,我使用的解决方案是:我删除了 id 列并将 objectHash 设置为 PK,然后 save_bulk 和 add_all 函数工作并实际进行了批量插入。 It seems like those functions work only if you already have the pk inside the object.似乎只有当您已经在对象中拥有 pk 时,这些功能才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM