优化 python 代码以获得更快的响应

optimize python code for quicker response

此代码有效,但速度很慢。我想使用 sqlalchemy 模块,因为脚本的其余部分使用它而不是 mysql。使用 sqlalchemy 有什么好处,还是我应该继续使用这个 ...

for emp_id in mylist:
    try:
        connection = mysql.connector.connect(host='x.x.x.x', port='3306', database='xxx', user='root', password='xxx')

        cursor = connection.cursor(prepared=True)
        sql_fetch_blob_query = """SELECT col1, col2, Photo from tbl where ProfileID = %s"""

        cursor.execute(sql_fetch_blob_query, (emp_id, ))
        record = cursor.fetchall()
        for row in record:
            image =  row[2]
            file_name = 'myimages4'+'/'+str(row[0])+ '_' + str(row[1]) + '/' + 'simage' + str(emp_id) + '.jpg'
            write_file(image, file_name)

    except mysql.connector.Error as error :
        connection.rollback()
        print("Failed to read BLOB data from MySQL table {}".format(error))

    finally:
        if(connection.is_connected()):
            cursor.close()
            connection.close()            

您真的需要设置新的 mysql 连接并在每次迭代时获取游标吗?如果没有,一开始就打开一次,真的会加速你的代码。

connection = mysql.connector.connect(host='x.x.x.x', port='3306', database='xxx', user='root', password='xxx', charset="utf8")
cursor = connection.cursor(prepared=True)

for emp_id in mylist:
    try:
        sql_fetch_blob_query = """SELECT col1, col2, Photo from tbl where ProfileID = %s"""

        cursor.execute(sql_fetch_blob_query, (emp_id, ))
        record = cursor.fetchall()
        for row in record:
            image =  row[2]
            file_name = 'myimages4'+'/'+str(row[0])+ '_' + str(row[1]) + '/' + 'simage' + str(emp_id) + '.jpg'
            write_file(image, file_name)

    except mysql.connector.Error as error :
        connection.rollback()
        print("Failed to read BLOB data from MySQL table {}".format(error))

    finally:
        # ouch ...
        if(connection.is_connected()):
            cursor.close()
            connection.close()   

更新: 其实你甚至不需要对数据库进行N次查询,因为用WHERE ProfileID IN (.., ..) SQL语句一次查询就可以得到所有的数据。看看这个小代码,它解决了一个几乎相同的任务:

transaction_ids = [c['transaction_id'] for c in checkouts]
format_strings = ','.join(['%s'] * len(transaction_ids))
dm_cursor.execute("SELECT ac_transaction_id, status FROM transactions_mapping WHERE ac_transaction_id IN (%s)" % format_strings, tuple(transaction_ids)) 
payments = dm_cursor.fetchall()

请用它来解决您的问题。