Setting MySQL session variable with sqlalchemy in python

安稳与你 提交于 2020-01-24 20:32:25

问题


I have a MySQL query that gives the error: "Error Code: 1104. The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay"

To run this successfully in MySQL Workbench, I simply execute:

SET SESSION SQL_BIG_SELECTS = 1;
SELECT ...
FROM ...
WHERE ...;

This runs both statements at once - SQL_BIG_SELECTS is set to 1 which allows the query below it to run.

Now I want to access the result of this query in python and store it in a pandas dataframe. I'm using mysqlalchemy to connect to the database.

I tried running the SET and SELECT at the same time, the same way I am able to successfully in MySQL Workbench:

from sqlalchemy import create_engine

def query001():
  engine_fdb = create_engine('...connection string...')

  sql_cmd = """
    SET SESSION SQL_BIG_SELECTS = 1;
    SELECT ...
    FROM ...
    WHERE ...;
  """

  df = pd.read_sql(sql_cmd, engine_fdb)

The last line generates an error: "sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically." I'm having a hard time understanding the information I've found on why this happens - I feel I'm in over my head.

In python, how can I query a MySQL database, first setting the option SET SESSION SQL_BIG_SELECTS = 1 and then executing a query with that option turned on?

来源:https://stackoverflow.com/questions/40514796/setting-mysql-session-variable-with-sqlalchemy-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!