【问题标题】:Import huge data set csv in MYSQL在 MYSQL 中导入巨大的数据集 csv
【发布时间】:2014-08-08 03:17:01
【问题描述】:

我正在尝试从一个约 400MB 的 csv 文件中导入一个巨大的数据集,其中包含 900000 行。这个文件有两个关系表的信息。例如:

["primary_key","name","lastname","phone,"work_id","work_name"]

每一行我都必须检查主键是否存在以进行插入或更新,如果需要,我还需要验证工作,因为新作品可以出现在这个数据集中。

我的 person 表比 csv 文件有更多的列,所以我不能用 mysqlimport 替换该行。

关于如何使用它的任何想法?

【问题讨论】:

标签: mysql csv load-data-infile


【解决方案1】:

Please read the documentation for LOAD DATA INFILE;它是加载数据的好选择,即使是非常大的文件。引用Reference manual: Speed of insert statements:

从文本文件加载表格时,使用LOAD DATA INFILE。这通常比使用 INSERT 语句快 20 倍

假设您的表比.csv 文件的列多,那么您必须编写如下内容:

load data local infile 'path/to/your/file.csv'
into table yourTable
fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'
ignore 1 lines -- if it has column headers
(col1, col2, col3, ...) -- The matching column list goes here

my own question on the subject and its answer

另外,如果您需要更快的插入,您可以:

  • 忽略外键约束,在执行load data之前使用SET foreign_key_checks = 0;,和/或
  • 在执行load data之前用alter table yourTable disable keys;禁用表的索引,然后用alter table yourTable enable keys;重建它们

未经测试:如果您的.csv 文件的列比您的表多,我认为您可以将文件中的“超出”列分配给临时变量:

load data local infile 'path/to/your/file.csv'
into table yourTable
fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'
ignore 1 lines -- if it has column headers
(col1, col2, col3, @dummyVar1, @dummyVar2, col4) -- The '@dummyVarX` variables
                                                 -- are simply place-holders for
                                                 -- columns in the .csv file that
                                                 -- don't match the columns in 
                                                 -- your table

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 2018-11-21
    • 1970-01-01
    • 2013-12-10
    • 2015-09-08
    • 2011-10-19
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    相关资源
    最近更新 更多