mysql:重复条目“0”对于键“PRIMARY”;以及奇怪的身份行为

发布于 2024-08-24 05:44:10 字数 2611 浏览 10 评论 0原文

请参阅下面的日志。 (为了简洁而剪掉;取消剪掉@ http://pastebin.com/k9sCM6Ee

简而言之:不知何故,行是获得分配的 ID 0。发生这种情况时,它会阻止插入,即使这些插入实际上并不与 ID 0 冲突(尽管这种情况一开始就不应该发生)。

尽管该表的读取量和插入量非常大(高达约 300k 行/分钟),但该表从未更新。唯一的插入方法是导致如下所示的 INSERT INTO 查询的方法。没有外键等。

a) WTF? b) 我该如何解决这个问题?

谢谢!

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.30, for apple-darwin9.4.0 (i386) using readline 5.1

$ mysql

mysql> SHOW CREATE TABLE visitations \G
*************************** 1. row ***************************
       Table: visitations
Create Table: CREATE TABLE `visitations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `scraping_id` int(11) NOT NULL,
  `site_id` int(11) NOT NULL,
  `visited` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_visitations_on_scraping_id_and_site_id` (`scraping_id`,`site_id`),
  KEY `index_visitations_on_site_id` (`site_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23525407 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> show triggers;
Empty set (0.04 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

mysql> SELECT *  FROM `visitations` WHERE  (`scraping_id`,`site_id`,`visited`) IN ((647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip]);
Empty set (1 min 27.43 sec)

mysql> select * from visitations where id = 0;
+----+-------------+---------+---------+
| id | scraping_id | site_id | visited |
+----+-------------+---------+---------+
|  0 |         645 |   46177 |       0 | 
+----+-------------+---------+---------+
1 row in set (0.00 sec)

mysql> delete from visitations where id < 363;
Query OK, 363 rows affected (0.11 sec)

mysql> select * from visitations where id = 0;
Empty set (0.00 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
Query OK, 500 rows affected (0.23 sec)
Records: 500  Duplicates: 0  Warnings: 0

mysql> select * from visitations where id = 0;
Empty set (0.00 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
ERROR 1062 (23000): Duplicate entry '647-196' for key 'index_visitations_on_scraping_id_and_site_id'

See log below. (Snipped just for brevity; unsnipped @ http://pastebin.com/k9sCM6Ee)

In short: somehow rows are getting assigned ID 0. When this happens, it blocks inserts, even when those inserts aren't actually conflicting with ID 0 (although that really shouldn't happen in the first place).

Although it is heavily read and very heavily inserted (up to ~300k rows/min), this table is never updated. The only method that inserts is the one that results in the INSERT INTO queries like below. There are no foreign keys or the like.

a) WTF?
b) How do I fix it?

Thanks!

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.30, for apple-darwin9.4.0 (i386) using readline 5.1

$ mysql

mysql> SHOW CREATE TABLE visitations \G
*************************** 1. row ***************************
       Table: visitations
Create Table: CREATE TABLE `visitations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `scraping_id` int(11) NOT NULL,
  `site_id` int(11) NOT NULL,
  `visited` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_visitations_on_scraping_id_and_site_id` (`scraping_id`,`site_id`),
  KEY `index_visitations_on_site_id` (`site_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23525407 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> show triggers;
Empty set (0.04 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

mysql> SELECT *  FROM `visitations` WHERE  (`scraping_id`,`site_id`,`visited`) IN ((647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip]);
Empty set (1 min 27.43 sec)

mysql> select * from visitations where id = 0;
+----+-------------+---------+---------+
| id | scraping_id | site_id | visited |
+----+-------------+---------+---------+
|  0 |         645 |   46177 |       0 | 
+----+-------------+---------+---------+
1 row in set (0.00 sec)

mysql> delete from visitations where id < 363;
Query OK, 363 rows affected (0.11 sec)

mysql> select * from visitations where id = 0;
Empty set (0.00 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
Query OK, 500 rows affected (0.23 sec)
Records: 500  Duplicates: 0  Warnings: 0

mysql> select * from visitations where id = 0;
Empty set (0.00 sec)

mysql> INSERT INTO `visitations` (`scraping_id`,`site_id`,`visited`) VALUES (647,196,0),(647,51679,0),(647,13689,0),(647,85739,1),(647,4388,0),(647,100346,0),(647,1245,0),[snip];
ERROR 1062 (23000): Duplicate entry '647-196' for key 'index_visitations_on_scraping_id_and_site_id'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-08-31 05:44:10

您可能遇到了这样的错误:

更新后自动增量无法与 InnoDB 正常工作

您需要跟踪您正在使用的版本的更改历史记录,以确定修复的错误是否会影响您以及是否应该升级。

MySQL 5.1 更改历史记录

You have probably hit a bug like:

auto increment does not work properly with InnoDB after update

You need to track the change history from the release you are using to identify whether fixed bugs can affect you and whether you should upgrade.

MySQL 5.1 Change History

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文