我正在尝试使用查询API创建具有MAX_EXECUTION_TIME(30000)提示的MySQL查询。
我的代码大致是:
from flask_sqlalchemy import SQLAlchemy
class MyTable(SQLAlchemy().Model):
...
base_query = Sqlalchemy.session.query(MyTable) # This works when I execute it.
base_query_with_hint = base_query.with_hint(MyTable, "MAX_EXECUTION_TIME(30000)")
base_query_with_hint.execute() # THIS DOES NOT WORK.
str(base_query_with_hint)
# Output
# Select a, b, c from MyTable MAX_EXECUTION_TIME(30000)
很明显,提示只是在查询结束时呈现!
应呈现为
Select /*+ MAX_EXECUTION_TIME(30000) */ a, b, c from MyTable
我希望它的行为与https://docs.sqlalchemy.org/en/13/core/selectable.html#sqlalchemy.sql.expression.Select.with_hint完全一样,但是我使用的是Query API而不是Select API。
答案 0 :(得分:0)
简而言之,请改用prefix_with()
。
下面是来自SQLAlchemy documentation的MySQL 5.7优化器提示的类似示例,该示例在Python交互式提示中运行:
>>> from sqlalchemy.sql import select, table
>>> from sqlalchemy.dialects import mysql
>>> tbl = table('my_table')
>>> # MySQL 5.7 optimizer hints
>>> stmt = select([tbl]).prefix_with(
... "/*+ MAX_EXECUTION_TIME(1000) */", dialect="mysql")
>>> print(stmt.compile(dialect=mysql.dialect()))
SELECT /*+ MAX_EXECUTION_TIME(1000) */
FROM my_table
>>>