Python 如何分批次导入 100W 条 mysql 数据

2017-04-22 18:29:47 +08:00
 wangyu8460958

问题 1 :现在我需要使用 Python 脚本导入 100W 条 mysql 数据,如果要每一次插入 1W 条数据(即每1W条数据提交1次),共插入 100 次,在批量操作中不开启事务, python 脚本应该如何来写?

问题 2 :对于下面的语句,想问一下 conn.commit()是不是把上面的 sql 语句做为事务来提交?

#coding=utf-8 import MySQLdb

conn = MySQLdb.connect(host='localhost',port = 3306,user='admin',passwd='admin',db='google') cur = conn.cursor() try: create_tb_cmd=''' create table if not exists tmp.Video_2017bak (id int(10) unsigned, asset_id int(10) unsigned, company_id int(10), ); ''' cur.execute(create_tb_cmd) finally: insert_tb_cmd=''' insert ignore into tmp.Video_2017bak (select * from google.Video where created_at < date_sub(now(), interval 2 year));''' delete_tb_cmd=''' delete from google.Video where created_at < date_sub(now(), interval 2 year);''' cur.execute(insert_tb_cmd) cur.execute(delete_tb_cmd)

cur.close() conn.commit() conn.close()

2450 次点击
所在节点    Python
4 条回复
skydiver
2017-04-22 19:40:40 +08:00
直接用 MySQL 插入,没必要用 Python
wangyu8460958
2017-04-22 20:49:13 +08:00
@skydiver 我想使用脚本让它每周执行一次,所以想使用 python 脚本来做。
billlee
2017-04-22 23:27:47 +08:00
这代码没法看
我搞不清楚 autocommit 默认是不是开启的,所以我会显示地禁用掉。
禁用 autocommit 的情况下,就是写个循环执行 INSERT, 然后搞个计数器,每 10000 条执行一次 conn.commit() 就行了
wangyu8460958
2017-04-27 15:53:42 +08:00
下面是我自己写的脚本,下面的脚本还能够优化吗?

#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()

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/356675

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX