Python SQLAlchemy如何批量调用存储的func
我需要连续数百次调用存储函数, 在一次往返DB的过程中这样做将是一个巨大的进步, 以下是我想在伪代码中执行的操作:Python SQLAlchemy如何批量调用存储的func,python,sqlalchemy,Python,Sqlalchemy,我需要连续数百次调用存储函数, 在一次往返DB的过程中这样做将是一个巨大的进步, 以下是我想在伪代码中执行的操作: args_for_multiple_calls = [ {"a1": 1, "a2": "dfg4"), {"a1": 4, "a2": "ger"), {"a1": 2, "a2": "sfg3"), ] connection.executemany("select my_stored_func(:a1, :a2)") SQLAlchemy是否支持此功能?SQLA
args_for_multiple_calls = [
{"a1": 1, "a2": "dfg4"),
{"a1": 4, "a2": "ger"),
{"a1": 2, "a2": "sfg3"),
]
connection.executemany("select my_stored_func(:a1, :a2)")
SQLAlchemy是否支持此功能?SQLAlchemy没有此类功能。这些“低级”命令如:insert、insertmany、callproc、execute、cursor等。。“由一个DBAPI()驱动程序提供,如cx Oracle、psycopg2、MySQL Python等。SQLAlchemy只是代理DBAPI驱动程序提供的这些功能 如果需要执行函数,应使用游标方法:
args_for_multiple_calls = [
{"a1": 1, "a2": "dfg4"),
{"a1": 4, "a2": "ger"),
{"a1": 2, "a2": "sfg3"),
]
conn = engine.raw_connection()
try:
cursor = conn.cursor()
for x in args_for_multiple_calls:
cursor.callproc("my_stored_func", [x['a1'], x['a2']])
results = list(cursor.fetchall())
conn.commit()
cursor.close()
finally:
conn.close()
SQLAlchemy提供了
executemany()
,作为以下内容的一部分:
executemany()
是否真正支持SELECT
查询取决于您的驱动程序。同时请记住,虽然这可能会减少往返开销,但由于查询执行开销,它可能仍然很慢(也就是说,如果您的存储过程只执行一次INSERT
,并且您运行了1000次存储过程,那么它将比一次包含1000行的INSERT
慢)。谢谢,但是如果我在args\u中有1000行用于多次调用,我将调用db 1000次,对吗?我想在一次db往返中执行此操作
session.execute("select my_stored_func(:a1, :a2)", args_for_multiple_calls)