如何使用SQLAlchemy ORM在Oracle中获得解释计划

时间:2018-09-18 17:55:27

标签: python oracle sqlalchemy

我想使用SQLAlchemy ORM来获取Oracle查询的解释计划。

这将在sqlplus中使用看起来像这样的2个步骤完成:

# step 1  
explain plan for select * from table_xyz;

# step 2
select * from table(dbms_xplan.display(null, null, 'SERIAL'));

到目前为止,我已经尝试使用SQLAlchemy ORM进行了以下尝试而没有运气:

from sqlalchemy import create_engine, Table
from sqlalchemy.orm import sessionmaker
import os

# create engine
user, pswd = (os.environ['USER'], os.environ['PW'])
conn_str = f'oracle://{user}:{pswd}@myDbService'
engine =  create_engine(conn_str, echo=True)

# create session
Session = sessionmaker(bind=engine)

# reflect existing table
MyTable = Table('my_table', meta, autoload=True, autoload_with=engine, schema='myschema')

# generate query
bind_param = dict('state_cd'=['CA', 'WA'])
query = str(Session().query(MyTable).filter(MyTable.c.my_state_cd.in_(state_cd)))
# print(query) # <-- this returns a properly formulated select query with bound parameters
result = Session().execute('EXPLAIN PLAN FOR ' + query, bind_param)

执行上面的最后一行一直失败,并显示以下错误,我不确定自己在做什么错了:

StatementError: (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'my_state_cd_2'

0 个答案:

没有答案