I have a rather long query (there were 7 joins, now 7 subqueries, because in raw sql 7 the subselects were much faster - I donβt even know when 7 joins would end if I let it work, but more than 1 min against 0, 05 seconds. With subqueries)
When I run it on db, it, as I said, takes .05-.1 seconds to execute. Just using session.execute() slows it down to a minute!
Is there anything I can do?
Let me know if you need more information. I suspect this is a common sqlalchemy thing. Perhaps sqlalchemy is setting up a query plan, and not just allowing mysql to do this? Or...?
EDIT: run the explanation for both, and they seem identical, except that sqlalchemy adds "using temporary using filesort" to the extra column. Is that what slows it down? How can I stop this from doing this?
EDIT 2: DEFINITELY sqlalchemy. I tried using the MySQL cursor to execute instead of the SA session and got the same .05 second runtime.
EDIT 3:
Code for creating our engine:
engine_ro = create_engine( config.ro_database_url, #string with username, password, db pool_size=config.database_pool_size, #int max_overflow=config.database_max_overflow, #int pool_timeout=config.database_timeout, # int echo=config.database_echo, #False echo_pool=config.database_echo, #same as echo #False listeners=[GoneAway()] if config.database_use_listeners else None)
where GoneAway() is a method that executes SELECT 1 to test the connection.
To create a session object:
SessionRO = scoped_session(sessionmaker(bind=engine_ro, autocommit=False))
where scoped_session and sessionmaker are sqlalchemy functions.
Then the code that executes the request:
session = SessionRO() results = session.execute(sql, params)
EDIT 4: In case someone wonders if I comment on the listeners bit, it is still slow. Also, if I just use sessionmaker without scoped_session.