LOAD DATA INFILE 主键上的重复条目 auto_increment id

huangapple go评论61阅读模式
英文:

LOAD DATA INFILE Duplicate entry on PRIMARY KEY auto_increment id

问题

我正在进行一个涉及在我的MySQL数据库上运行大量查询(搜索、更新和插入)的大数据项目。数据库每秒接收大约10个请求。此外,我需要每天将大批记录(每批约10万条记录)插入数据库。虽然这个过程通常运行良好,但我遇到了主键上的“重复条目”异常,主键是具有自动增量功能的 'id' 列。

令人困惑的是,'id' 列未包含在LOAD DATA INFILE语句中,因此不清楚在MySQL负责生成键时,为什么主键会出现重复条目异常。

以下是关于我的设置的一些关键细节:

表结构:

create table urls
(
    id           bigint unsigned auto_increment primary key,
    type         tinyint unsigned             null,
    queuemktime  int unsigned     default '0' not null,
    url          varchar(1024)                null,
    subdomain    varchar(256)                 null,
    domain       varchar(256)                 null,
    tld          varchar(256)                 null,
    country      varchar(2)       default ''  not null,
    constraint url
        unique (url)
)
    collate = utf8_bin
    auto_increment = 745448895;

LOAD DATA INFILE语句:

LOAD DATA INFILE '/path/to/batch_1.csv' IGNORE INTO TABLE urls FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (url, subdomain, domain, tld, country);

批处理文件的示例内容:

http://blog.scrimmage.co/,blog,scrimmage.co,.co,XX
http://corporate.tastyk.us/,corporate,tastyk.us,.us,XX
http://store.vmrtruckparts.com.au/,store,vmrtruckparts.com.au,.com.au,XX

我收到的异常如下:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '745285936' for key 'urls.PRIMARY'

对于为什么尽管'id'列具有自动增量功能,但仍然发生重复条目异常的原因,我将非常感激任何见解或建议。在此先感谢您的帮助!

英文:

I'm working on a big-data project that involves running numerous queries (searches, updates, and inserts) on my MySQL database. The database receives approximately 10 requests per second. Additionally, I need to insert large batches of records (around 100,000 records per batch) into the database on a daily basis. While this process generally works fine, I'm encountering "Duplicate entry" exceptions on the primary key, which is the 'id' column with auto-increment functionality.

The confusing part is that the 'id' column is not included in the LOAD DATA INFILE statement, so it's unclear how a duplicate entry exception is occurring on the primary key when MySQL is responsible for generating the key.

Here are some key details about my setup:

Table structure:

create table urls
(
    id           bigint unsigned auto_increment primary key,
    type         tinyint unsigned             null,
    queuemktime  int unsigned     default '0' not null,
    url          varchar(1024)                null,
    subdomain    varchar(256)                 null,
    domain       varchar(256)                 null,
    tld          varchar(256)                 null,
    country      varchar(2)       default ''  not null,
    constraint url
        unique (url)
)
    collate = utf8_bin
    auto_increment = 745448895;

LOAD DATA INFILE statement:

LOAD DATA INFILE '/path/to/batch_1.csv' IGNORE INTO TABLE urls FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (url, subdomain, domain, tld, country);

Sample content of the batch file:

http://blog.scrimmage.co/,blog,scrimmage.co,.co,XX
http://corporate.tastyk.us/,corporate,tastyk.us,.us,XX
http://store.vmrtruckparts.com.au/,store,vmrtruckparts.com.au,.com.au,XX

The exception I'm receiving is as follows:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '745285936' for key 'urls.PRIMARY'

I would greatly appreciate any insights or suggestions on why this duplicate entry exception is occurring despite the auto-increment functionality of the 'id' column. Thank you for your help in advance!

答案1

得分: 1

经过尝试了几种方法,我找到了解决方案。尽管我没有预料到,但实际上是MySQL本身导致了重复的auto_increment id。

虽然我仍然不确切知道为什么这是修复方法,但在将系统变量innodb_autoinc_lock_mode设置为1(“连续”锁定模式)后,问题得到了解决。默认情况下,它是2(“交错”锁定模式),这是自8.0版本以来的默认设置。我唯一能想到的是,连续锁定模式可能会更安全一些,但性能稍差一点,但我没有注意到性能差异。

https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html#innodb-auto-increment-lock-modes

英文:

After trying several things I found the solution. While I did not expect it, MySQL itself was the cause of the duplicate auto_increment id's.

While I still do not exactly know why this is the fix, the problem was solved after setting the system variable innodb_autoinc_lock_mode to 1 (“consecutive” lock mode). It was 2 (“interleaved” lock mode), which is the default since of version 8.0. The only thing I could think of is that the consecutive lock mode is a bit safer but performs a little less, but I don't notice it.

https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html#innodb-auto-increment-lock-modes

huangapple
  • 本文由 发表于 2023年6月5日 19:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定