V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wangyu8460958
V2EX  ›  Python

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

  •  
  •   wangyu8460958 · 2017-04-22 18:29:47 +08:00 · 2445 次点击
    这是一个创建于 2559 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题 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()

    4 条回复    2017-04-27 15:53:42 +08:00
    skydiver
        1
    skydiver  
       2017-04-22 19:40:40 +08:00 via Android
    直接用 MySQL 插入,没必要用 Python
    wangyu8460958
        2
    wangyu8460958  
    OP
       2017-04-22 20:49:13 +08:00
    @skydiver 我想使用脚本让它每周执行一次,所以想使用 python 脚本来做。
    billlee
        3
    billlee  
       2017-04-22 23:27:47 +08:00
    这代码没法看
    我搞不清楚 autocommit 默认是不是开启的,所以我会显示地禁用掉。
    禁用 autocommit 的情况下,就是写个循环执行 INSERT, 然后搞个计数器,每 10000 条执行一次 conn.commit() 就行了
    wangyu8460958
        4
    wangyu8460958  
    OP
       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()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1017 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:02 · PVG 06:02 · LAX 15:02 · JFK 18:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.