python 事务执行10w+插入更新代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihaitao_1/article/details/82112299

python 事务执行10w+插入更新代码

 之前一条条插入更新10w+大概执行了四五十分钟,后面组合sql语句,一次插入200条节省了十几分钟而已,后面用事物,大概两分钟,效率好了很多。这是主要逻辑代码,数据修改成自己的就可以了,如果不行,可以微信加我li1236li。

代码块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb

import time
import datetime

connection = MySQLdb.connect(host='127.0.0.1',
                            port='3306',
                            user='root',
                            passwd='123456',
                            db='test')

def merge_sql(table,symbol,src,data):
    sql = """ insert into %s set symbol='%s',close_pri=%f,
                    open_pri=%f,max_pri=%f,min_pri=%f,total_vol=%f,
                    update_time=%d, source='%s' on duplicate key update symbol='%s',close_pri=%f,
                    open_pri=%f,max_pri=%f,min_pri=%f,total_vol=%f
                    """ % (table, symbol, data['close']),  data['open'],  data['high'],  data['low'], data['total'], int(data['ts'],src, symbol,  data['close'],  data['open'],  data['high'],  data['low'], data['total'])
    return sql


def handle_binance():
    try:
        cursor = connection.cursor()
        start_time = time.time()  
        temp_list = []
        for i in range(0,1500): 
            temp_list = symbol_list[i:i+40] 
            print temp_list  
            for j in range(0,100):
                symbol = 'binance'
                sql = '' 
                #数据   
                data = {'ts':1,'close':i,'open':j,'high':1,'low':1,'total':1}
                    sql = merge_sql('tableName', symbol, 'binance', data) + ';'        
                    if sql == '':
                        continue        
                    cursor.execute(sql)
        connection.commit()
        connection.close()
        end_time = time.time()
        total_time = end_time - start_time
        print '数据花费总时间为:' + str(total_time) + '秒'
    except Exception as e:
        print e            

def main():
    try:
        handle_binance()
    except Exception as e:
        print e
if __name__ == '__main__':
    main()           


猜你喜欢

转载自blog.csdn.net/lihaitao_1/article/details/82112299