mysql - 删除重复行时超慢加载数据 infile

标签 mysql

我在 mysql 数据库中加载数据时遇到问题。我用它作为在我的数据库中插入数据的方法:

USE database;
ALTER TABLE country
ADD UNIQUE INDEX idx_name (`insee_code`,`post_code`,`city`);

LOAD DATA INFILE 'C:/wamp64/tmp/myfile-csv'
                         REPLACE
                         INTO TABLE `country` CHARACTER SET utf8
                         FIELDS TERMINATED BY ','
                         LINES TERMINATED BY '\r\n'
                         IGNORE 1 LINES;

虽然我的 table 很简单:

CREATE TABLE `country` (`insee_code`  VARCHAR(250),
                        `post_code`   VARCHAR(250),
                        `city`        VARCHAR(250));

在我使用 php 脚本加载其他表之前,它非常快(3 分钟内 3GB),但是对于这个,需要 17 分钟才能完成 加载 1 GB。

我不知道为什么,因为有了索引,一些行会丢失或损坏,我只是想知道为什么。如果有人有其他方法可以在从 CSV 加载数据时删除重复行,我将不胜感激。

提前致谢。

最佳答案

使用 REPLACE 基本上是先删除行,然后插入新行。您要做的是 IGNORE

The REPLACE and IGNORE keywords control handling of input rows that duplicate existing rows on unique key values:

  • If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row. See Section 13.2.9, “REPLACE Syntax”.

  • If you specify IGNORE, rows that duplicate an existing row on a unique key value are discarded. For more information, see Comparison of the IGNORE Keyword and Strict SQL Mode.

另外,如果能加个主键就更好了。如果你不这样做,MySQL 会隐式地为你创建一个。这个是不可见的,要么是 uuid 要么是 bigint。我记不太清楚了。无论如何,这不是最佳的性能和存储方式。执行这个:

ALTER TABLE country ADD column id int unsigned auto_increment primary key;

关于mysql - 删除重复行时超慢加载数据 infile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276769/

相关文章:

python - 如何将两个 JSON 附加到一个 JSON,Python Flask?

mysql - mysql中的只读表

mysql - 如果不满足查询中的条件,mysql 是否会移动到下一行?

javascript - 创建一个简单的密码登录,无需硬编码密码 [PHP、Javascript、MySQL]

MYSQL:重复更新时插入 - 获取当前列值

php - 创建并添加到 session

php - 缩放 phpBB?

php - 如何判断某人是否离开

MySQL 全文搜索 : how to find out in which column the search query has been found?

Mysql查询: selecting from two rows in the same field