sqlalchemy批量upsert

怎样使用sqlchemy 将pandas upsert至mysql数据库。目前我只能使用以下方法遍历df的每一行然后执行upsert。有没有什么方法可以批量upsert呢?ps:订单数据表的订单号字段为主键
from sqlalchemy import create_engine, MetaData
from sqlalchemy import Table
db_conn = create_engine(
    'mysql+pymysql://root:****@localhost:****/****', # 密码 端口即schma已隐藏
    max_overflow=0,  # 超过连接池大小外最多创建的连接
    pool_size=5,  # 连接池大小
    pool_timeout=30,  # 池中没有线程最多等待的时间,否则报错
    pool_recycle=60  # 多久之后对线程池中的线程进行一次连接的回收(重置)
)
df = pd.read_csv(r"D:\订单信息", engine='python', delimiter=",",encoding="gbk"}).fillna("")
df_dict = df.to_dict('records')
metadata = MetaData(db_conn)
user_table = Table('订单数据', metadata, autoload=True)
for d in df_dict:
    on_duplicate_key_stmt = insert(user_table).values(d).on_duplicate_key_update(d)
    db_conn.execute(on_duplicate_key_stmt)

db_conn.session.bulk_update_mappings(
    user_table,  # 当前映射类
    [*df_dict]  # 列表里套的是字典打散
)
db_conn.session.commit()

# 注意点 批量更新要的是ID字段 需要注意ID填写

sqlalchemy 不是很熟练 没有找到这个bulk_up_mappings,能麻烦写详细一点吗@FlyingPig_chen

在SQLAlchemy中有一个upsert类型的操作:

db.session.merge()

在我找到这个命令之后,我可以执行upsert,但是值得一提的是,对于大容量的“upsert”,这个操作很慢。

另一种方法是获取要追加插入的主键的列表,并查询数据库以获取任何匹配的ID:

# Imagine that post1, post5, and post1000 are posts objects with ids 1, 5 and 1000 respectively
# The goal is to "upsert" these posts.
# we initialize a dict which maps id to the post object

my_new_posts = {1: post1, 5: post5, 1000: post1000} 

for each in posts.query.filter(posts.id.in_(my_new_posts.keys())).all():
    # Only merge those posts which already exist in the database
    db.session.merge(my_new_posts.pop(each.id))

# Only add those posts which did not exist in the database 
db.session.add_all(my_new_posts.values())

# Now we commit our modifications (merges) and inserts (adds) to the database!
db.session.commit()