wangyu8460958 最近的时间轴更新
wangyu8460958

wangyu8460958

V2EX 第 227278 号会员,加入于 2017-04-22 10:01:56 +08:00
wangyu8460958 最近回复了
2017-04-27 15:53:42 +08:00
回复了 wangyu8460958 创建的主题 Python Python 如何分批次导入 100W 条 mysql 数据
下面是我自己写的脚本,下面的脚本还能够优化吗?

#coding=utf-8

import MySQLdb

import numpy as np

conn = MySQLdb.connect(host='localhost',port = 3306,user='root',passwd='123456',db='google')
cur = conn.cursor()
conn.autocommit(1)

query_sql = "select id from google.Video where created_at < date_sub(now(), interval 1 year);"
insert_sql = "insert ignore into tmp.Video_2017bak (select * from google.Video where id = %s);"
delete_sql = "delete from google.Video where id = %s;"
cur.execute(query_sql)
dataList = cur.fetchall()
aaa = np.array(dataList)
ids = []

for i in range(len(aaa)):
ids.append(aaa[i])
if (i+1)%100==0 :
cur.executemany(insert_sql,ids)
ids = []
cur.executemany(insert_sql,ids)
ids = []

for i in range(len(aaa)):
ids.append(aaa[i])
if (i+1)%100==0 :
cur.executemany(delete_sql,ids)
ids = []
cur.executemany(delete_sql,ids)
ids = []

cur.close()
conn.close()
2017-04-22 20:49:13 +08:00
回复了 wangyu8460958 创建的主题 Python Python 如何分批次导入 100W 条 mysql 数据
@skydiver 我想使用脚本让它每周执行一次,所以想使用 python 脚本来做。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5306 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 20ms · UTC 09:10 · PVG 17:10 · LAX 02:10 · JFK 05:10
Developed with CodeLauncher
♥ Do have faith in what you're doing.