con.execute(sql).fetchall()compiled = sql.compile(dialect=con.dialect)
statement = compiled.string
params = compiled.params
con.execute(statement, params).fetchall() def do_execute(self, cursor, statement, parameters, context=None):
cursor.execute(statement, parameters)Hello,given an SQLAlchemy engine `con` (cx_oracle dialect) and a moderately complex SQLAlchemy selectable object `sql`, the following code will consistently take ~15 seconds:con.execute(sql).fetchall()Whereas with the exact same engine and query the following code will only take a fraction of a second:compiled = sql.compile(dialect=con.dialect)
statement = compiled.string
params = compiled.params
con.execute(statement, params).fetchall()
So, apparently something goes horribly wrong when executing an selectable. I assume that there is something wrong on my side as a general error in SQLAlchemy in this regard would probably have been noticed. However: How do I even debug this? In the end, I did trace the problem down to the `do_execute` function in engine.default:def do_execute(self, cursor, statement, parameters, context=None):
cursor.execute(statement, parameters)
Both of my code examples visit this line of code with the exact same values for `statement` and `parameters`, though the way they get there is slightly different and so are the `cursor` and `context` objects. When I try to step further down in the debugger, the next frame would be `output_type_handler` in dialects.oracle.cx_oracle. However, the time is already lost before stepping into that function. That is, just entering this frame takes ~15 seconds in my first code but is near instantaneous in my second code.I do not really know how to continue debugging from this point. Anybody have any advice or any ideas what might be going on here? The only difference that looks even remotely relevant to me at that point is that within the `context` we have a value for `context.compiled` for the first code and None for the second code. I tried setting it to None in the debugger but that had no effect on the execution speed. The cursors look both the same to me in both codes.At this point I do not even know what else to look for and would appreciate any advice how to proceed. After not finding anything in the local context I would assume the problem is somewhere in some global state but without intimate knowledge of the inner working of SQLAlchemy I don't think I will be able to find the root cause.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---You received this message because you are subscribed to the Google Groups "sqlalchemy" group.To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/b6c933f0-5ad5-468d-ba15-ba49e9c3fb55%40googlegroups.com.For more options, visit https://groups.google.com/d/optout.
Thank you for the pointers! After digging into this some more, I found that this is definitely related to something in cartain versions of SQLAlchemy. I also tried different versions of cx_Oracle (6.4.1, 7.1.2, and 7.1.3) but that had no effect on the performance of the statement.I did not try all possible combinations of SQLAlchemy and cx_Oracle but I tried to do a little bisection on the SQLAlchemy version while keeping cx_Oracle fixed at 7.1.3. What I found:
- 1.3.0 and above: Fine!
- 1.2.19: slow
- 1.2.12: slow
- 1.2.6: slow
- 1.2.3: slow
- 1.2.2: slow
- 1.2.1: Fine
- 1.2.0: Fine
- 1.1,18: Fine
So, the culprint is SQLAlchemy 1.2.2 and 1.3.0 fixes whatever was wrong. While that solves my problem in an elegant way (I wanted to upgrade to 1.3+ anyway) I will still try to find the root cause by profiling my application the way you suggested. Maybe something can be learned from that to avoid future regressions. However, that will take some time. I will be back with the results, later!
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---You received this message because you are subscribed to the Google Groups "sqlalchemy" group.To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/1a2c2fdb-9de4-4679-b7a0-940974f6e507%40googlegroups.com.
Still, upgrading to 1.3.x solves my problem. Thanks for all the help!
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---You received this message because you are subscribed to the Google Groups "sqlalchemy" group.To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/28389615-4154-45cc-aa3f-03339485ef8a%40googlegroups.com.