python - 如何使用 python 更快地将行插入到 mysql 表中?
问题描述
我正在尝试找到一种更快的方法将数据插入到我的表中,该表最终应该有超过 1 亿行,我已经运行我的代码将近 24 小时,并且该表目前只输入了 900 万行并且仍在进步。
我的代码当前一次读取 300 个 csv 文件,并将数据存储在一个列表中,它会针对重复的行进行过滤,然后我使用 for 循环将列表中的条目作为元组放置并更新表中的一个元组时间。这个方法太费时间了,有没有办法批量插入所有行?我曾尝试在网上查找,但我正在阅读的方法似乎对我的情况没有帮助。
非常感谢,
大卫
import glob
import os
import csv
import mysql.connector
# MYSQL logon
mydb = mysql.connector.connect(
host="localhost",
user="David",
passwd="Sword",
database="twitch"
)
mycursor = mydb.cursor()
# list for strean data file names
streamData=[]
# This function obtains file name list from a folder, this is to open files
in other functions
def getFileNames():
global streamData
global topGames
# the folders to be scanned
#os.chdir("D://UG_Project_Data")
os.chdir("E://UG_Project_Data")
# obtains stream data file names
for file in glob.glob("*streamD*"):
streamData.append(file)
return
# List to store stream data from csv files
sData = []
# Function to read all streamData csv files and store data in a list
def streamsToList():
global streamData
global sData
# Same as gamesToList
index = len(streamData)
num = 0
theFile = streamData[0]
for x in range(index):
if (num == 301):
filterStreams(sData)
num = 0
sData.clear()
try:
theFile = streamData[x]
timestamp = theFile[0:15]
dateTime = timestamp[4:8]+"-"+timestamp[2:4]+"-"+timestamp[0:2]+"T"+timestamp[9:11]+":"+timestamp[11:13]+":"+timestamp[13:15]+"Z"
with open (theFile, encoding="utf-8-sig") as f:
reader = csv.reader(f)
next(reader) # skip header
for row in reader:
if (row != []):
col1 = row[0]
col2 = row[1]
col3 = row[2]
col4 = row[3]
col5 = row[4]
col6 = row[5]
col7 = row[6]
col8 = row[7]
col9 = row[8]
col10 = row[9]
col11 = row[10]
col12 = row[11]
col13 = dateTime
temp = col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13
sData.append(temp)
except:
print("Problem file:")
print(theFile)
print(num)
num +=1
return
def filterStreams(self):
sData = self
dataSet = set(tuple(x) for x in sData)
sData = [ list (x) for x in dataSet ]
return createStreamDB(sData)
# Function to create a table of stream data
def createStreamDB(self):
global mydb
global mycursor
sData = self
tupleList = ()
for x in sData:
tupleList = tuple(x)
sql = "INSERT INTO streams (id, user_id, user_name, game_id, community_ids, type, title, viewer_count, started_at, language, thumbnail_url, tag_ids, time_stamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = tupleList
try:
mycursor.execute(sql, val)
mydb.commit()
except:
test = 1
return
if __name__== '__main__':
getFileNames()
streamsToList()
filterStreams(sData)
解决方案
如果您的某些行成功但某些行失败,您是否希望您的数据库处于损坏状态?如果不是,请尝试退出循环。像这样:
for x in sData:
tupleList = tuple(x)
sql = "INSERT INTO streams (id, user_id, user_name, game_id, community_ids, type, title, viewer_count, started_at, language, thumbnail_url, tag_ids, time_stamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = tupleList
try:
mycursor.execute(sql, val)
except:
# do some thing
pass
try:
mydb.commit()
except:
test = 1
如果你不这样做。尝试将您的cvs
文件直接加载到您的mysql中。
LOAD DATA INFILE "/home/your_data.csv"
INTO TABLE CSVImport
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
另外,为了让你更清楚。如果您坚持使用 python,我已经定义了三种插入这些数据的方法,因为您对数据进行了一些处理。
不好的方式
In [18]: def inside_loop():
...: start = time.time()
...: for i in range(10000):
...: mycursor = mydb.cursor()
...: sql = "insert into t1(name, age)values(%s, %s)"
...: try:
...: mycursor.execute(sql, ("frank", 27))
...: mydb.commit()
...: except:
...: print("Failure..")
...: print("cost :{}".format(time.time() - start))
...:
时间成本:
In [19]: inside_loop()
cost :5.92155909538269
好的方式
In [9]: def outside_loop():
...: start = time.time()
...: for i in range(10000):
...: mycursor = mydb.cursor()
...: sql = "insert into t1(name, age)values(%s, %s)"
...: try:
...: mycursor.execute(sql, ["frank", 27])
...: except:
...: print("do something ..")
...:
...: try:
...: mydb.commit()
...: except:
...: print("Failure..")
...: print("cost :{}".format(time.time() - start))
时间成本:
In [10]: outside_loop()
cost :0.9959311485290527
也许,还有一些更好的方法,甚至是最好的方法。(即,用于pandas
处理您的数据。并尝试重新设计您的表格......)
推荐阅读
- django - 保存每个表单提交的日期 Django
- python - Power BI Python 视觉对象 - 我的时间数据是错误的还是我以错误的方式格式化它?
- cron - nifi处理器cron调度
- c++ - 优先级队列库中的 C++ 推送功能在我的环境中不起作用
- mysql - 在 VS Studio 中运行 join 语句时 SQL 更改 id
- r - 如何从 R 中的绘制线生成数据?
- flutter - Flutter,如何将字体大小选项保存到 sharedpreference 中?
- javascript - 如何检测网站的主要内容块?
- c++ - 在 Linux 中无需拔出即可软重置 USB 设备
- java - 带有 '\\?' 的正则表达式 符号在我的代码中不起作用