我有一个 3000 万行(1.6 gb)的大型 csv 文件,我正在使用 pymysql 将数据从 csv 加载到 mysql 表。我已经删除了表架构中的所有约束以加快加载速度,并将超时值设置为大值。
def setTimeOutLimit(connection):
try:
with connection.cursor() as cursor:
query = "SET GLOBAL innodb_lock_wait_timeout = 28800"
cursor.execute(query)
query2 = "SET innodb_lock_wait_timeout = 28800"
cursor.execute(query2)
query3 = "SET GLOBAL connect_timeout = 28800"
cursor.execute(query3)
query4 = "SET GLOBAL wait_timeout = 28800"
cursor.execute(query4)
query5 = "SET GLOBAL interactive_timeout = 28800"
cursor.execute(query5)
query6 = "SET GLOBAL max_allowed_packet = 1073741824"
cursor.execute(query6)
except:
conn.close()
sys.exit(" Could not set timeout limit ")
数据被插入到表中,但我需要将其中一个列作为主键,因此我正在创建另一个表,该表通过忽略重复值使该列成为主索引。(tableName_1 是旧表 tableName 是新表)
def createNewTableFromOld(connection, tableName):
try:
pprint( " Creating new table from old table with constraints" )
with connection.cursor() as cursor:
query = (" CREATE TABLE " + tableName +
" Like " + tableName + "_1")
cursor.execute(query)
query2 = (" ALTER TABLE " + tableName +
" ADD PRIMARY KEY(TimeStamp) ")
cursor.execute(query2)
query3 = (" INSERT IGNORE INTO " + tableName +
" SELECT * FROM " + tableName + "_1")
cursor.execute(query3)
query4 = ("DROP TABLE " + tableName + "_1")
cursor.execute(query4)
connection.commit()
except:
conn.close()
sys.exit(" Could not create table with Primary Key ")
在此方法执行期间,在 5-6 分钟后的某个地方我收到此错误, pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
而当我检查服务时,MYSQL80 自动崩溃并停止。我还在 my.ini 文件中将 max_allowed_packet_size 设置为 1 gb,并且所有超时都手动设置为 8 小时。可能是什么问题?
慕侠2389804
相关分类