简体   繁体   English

如何使用 jdbc 更快地将大量数据从 tsv 导入到 MySQL?

[英]How to import huge data from tsv to MySQL using jdbc faster?

I have two huge tsv files(10mil records) where tsv one file has the attributes id, name, age and other had attributes id, email and phno.我有两个巨大的 tsv 文件(1000 万条记录),其中一个 tsv 文件具有属性 id、name、age 和其他属性 id、email 和 phno。 I tried to read the first file and insert the records into the Person table and then read the second file and update the Person table.我尝试读取第一个文件并将记录插入到 Person 表中,然后读取第二个文件并更新 Person 表。 This approach takes time as the table is first inserted with 10 mil records and then they are updated.这种方法需要时间,因为首先在表中插入 1000 万条记录,然后对其进行更新。 IS there any other way to speed this process?有没有其他方法可以加快这个过程? PS some Id are not there in the 2nd tsv file so I was not able to merge both of them. PS第二个tsv文件中没有一些ID,所以我无法合并它们。

Why you don't try LOAD DATA INFILE which is a highly optimized, MySQL -specific statement that directly inserts data into a table from a CSV / TSV file.为什么不尝试LOAD DATA INFILE ,它是一个高度优化的MySQL特定语句,它直接将数据从CSV / TSV文件插入到表中。

There are two ways to use LOAD DATA INFILE .有两种方法可以使用LOAD DATA INFILE You can copy the data file to the server's data directory (typically /var/lib/mysql-files/ ) and run:您可以将数据文件复制到服务器的数据目录(通常是/var/lib/mysql-files/ )并运行:

LOAD DATA INFILE '/path/to/products.csv' INTO TABLE products;

Or you can also store the data file on the client side, and use the LOCAL keyword:或者你也可以在客户端存储数据文件,并使用 LOCAL 关键字:

LOAD DATA INFILE '/path/to/products.csv' INTO TABLE products;

High-speed inserts with MySQL 带 MySQL 的高速刀片

You should also check MySql Documentation - LOAD DATA Statement And you could use a statement like this one:您还应该检查MySql 文档 - 加载数据语句您可以使用如下语句:

LOAD DATA INFILE 'data.txt' 
  INTO TABLE tbl_name
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM