python多进程读写数据库问题_Python:将多进程插入MySQL的代码有什么问题?

I've write a Python script to insert some data(300 millions) to a MySQL table:

#!/usr/bin/python

import os

import MySQLdb

from multiprocessing import Pool

class DB(object):

def __init__(self):

self.conn = MySQLdb.connect(host='localhost',user='root',passwd='xxx',db='xdd',port=3306)

self.cur = self.conn.cursor()

def insert(self, arr):

self.cur.execute('insert into RAW_DATA values(null,%s,%s,%s,%s,%s,%s,%s)', arr)

def close(self):

self.conn.commit()

self.cur.close()

self.conn.close()

def Import(fname):

db = DB()

print 'importing ', fname

with open('data/'+fname, 'r') as f:

for line in f:

arr = line.split()

db.insert(arr)

db.close()

if __name__ == '__main__':

# 800+ files

files = [d for d in os.listdir('data') if d[-3:]=='txt']

pool = Pool(processes = 10)

pool.map(Import, files)

The problem is, the script runs very very slow, is there any obvious wrong using of multiprocessing ?

解决方案

Yes, if you are bulk-inserting 300 million rows into the same table, then you shouldn't try to parallelize this insertion. All inserts must go through the same bottlenecks: updating the index, and writing into the physical file on the hard disk. These operations require exclusive access to the underlying resources (the index, or the disk head).

You are actually adding some useless overhead on the database which now has to handle several concurrent transactions. This consumes memory, forces context switching, makes the disk read head jump around all the time, and so on.

Insert everything in the same thread.

It looks like you are actually importing data from a kind of CSV file. You may want to use the built-in LOAD DATA INFILE MySQL command, designed for this very purpose. Please describe your source file if you need some help in tuning this command.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值