推荐学习书目
› 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 · Apr 22, 2017 · 3306 views
    This topic created in 3442 days ago, the information mentioned may be changed or developed.

    问题 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 replies  •  2017-04-27 15:53:42 +08:00
    skydiver
        1
    skydiver  
       Apr 22, 2017 via Android
    直接用 MySQL 插入,没必要用 Python
    wangyu8460958
        2
    wangyu8460958  
    OP
       Apr 22, 2017
    @skydiver 我想使用脚本让它每周执行一次,所以想使用 python 脚本来做。
    billlee
        3
    billlee  
       Apr 22, 2017
    这代码没法看
    我搞不清楚 autocommit 默认是不是开启的,所以我会显示地禁用掉。
    禁用 autocommit 的情况下,就是写个循环执行 INSERT, 然后搞个计数器,每 10000 条执行一次 conn.commit() 就行了
    wangyu8460958
        4
    wangyu8460958  
    OP
       Apr 27, 2017
    下面是我自己写的脚本,下面的脚本还能够优化吗?

    #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()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2509 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 09:38 · PVG 17:38 · LAX 02:38 · JFK 05:38
    ♥ Do have faith in what you're doing.