优化python代码以更快地响应

时间:2019-05-01 13:42:44

标签: python mysql-connector-python

此代码有效,但速度很慢。而且我想使用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()            

1 个答案:

答案 0 :(得分:1)

您真的需要建立新的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()   

UPD: 实际上,您甚至不需要对数据库进行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()

请使用它来解决您的问题。