简体   繁体   中英

Why does removing this else make my code run so much faster?

I'm going through a database with python and using try and except a lot to handle queries. In trying to optimize my code, I've hit a weird bump.

This code:

try:
    cursor.execute("SELECT my_name FROM {}.{} LIMIT 1".format(myschema,mytable))
except(Exception, psycopg2.DatabaseError) as error:
    conn.rollback()
else:
    origName = cursor.fetchone()
    if origName is None:
        outputName = "ERROR2"
        return outputName
try:
    cursor.execute("SELECT different_column FROM {}.{} ORDER by a_column DESC LIMIT 1".format(myschema, mytable))
except(Exception, psycopg2.DatabaseError) as error:
    conn.rollback()
    try:...
        #more try/excepts and so on

takes about 19 minutes to run through the entire data warehouse.

but THIS code:

try:
    cursor.execute("SELECT my_column FROM {}.{} LIMIT 1".format(myschema,mytable))
except(Exception, psycopg2.DatabaseError) as error:
    conn.rollback()

origName = cursor.fetchone()
if origName is None:
    outputName = "ERROR2"
    return outputName
try:
    cursor.execute("SELECT different_column FROM {}.{} ORDER by a_column DESC LIMIT 1".format(myschema, mytable))
except(Exception, psycopg2.DatabaseError) as error:
    conn.rollback()
    try:...
        #exact same code with try/excepts and so on

literally completes running in about 1-1.5 minutes. Why is it so much faster if I remove that else? Is something being skipped/ignored? I feel like I must be doing wrong. I don't really understand how to just continue with my code after a try:except: statement.

This is because in the python world try-except blocks are very common and incredibly cheap to use. A try-except is cheaper than a false if statement if a != 1: .

The else-clause itself is interesting. It runs when there is no exception but before the finally-clause. That is its primary purpose. So if your try-except block doesn't catch, the else statement still runs. This is the primary reason it slows down your code.

Without the else-clause, the only option to run additional code before finalization would be the clumsy practice of adding the code to the try-clause. That is clumsy because it risks raising exceptions in code that wasn't intended to be protected by the try-block.

The use-case of running additional unprotected code prior to finalization doesn't arise very often. So, don't expect to see many examples in published code. It is somewhat rare.

Look at this post for some more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM