I'm trying to use the query API to create a MySQL query with the the MAX_EXECUTION_TIME(30000) hint.
My code is roughly:
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)
The hint is just rendered at the end of the query, apparently!
It should render as
Select /*+ MAX_EXECUTION_TIME(30000) */ a, b, c from MyTable
I want it to behave exactly like https://docs.sqlalchemy.org/en/13/core/selectable.html#sqlalchemy.sql.expression.Select.with_hint but I'm using the Query API instead of the Select API.
Another possibility: A commenter in stackoverflow suggested this is an issue with the MySQL dialect not the query API.
query = ... # my complex query, includes params that do not render with literal_bind.
# Directly edit query text and rebuild to add MAX_EXECUTION_TIME hint
query_statement_compiled = query.statement.compile()
query_binds = [bindparam(key=name, value=bp.value, type_=bp.type) for bp, name in query_statement_compiled.bind_names.iteritems()]
query_str = str(query_statement_compiled).replace('"', '`')
sel_ind = query_str.index('SELECT')
# Insert the hint directly after '...SELECT'
hint_str = ' /*+ MAX_EXECUTION_TIME(1000) */'
# hint_str = ''
query_str_with_hint = query_str[:sel_ind + 6] + hint_str + query_str[sel_ind+6:]
query_stmt_with_hint = text(query_str_with_hint, bindparams=query_binds)
query = db.session.query(ProvenancePrototype).from_statement(query_stmt_with_hint)
query.all() # Fails.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.