MySQL 将组合唯一索引截断为 64 个字符

发布于 2025-01-07 13:30:30 字数 1474 浏览 0 评论 0原文

我在使用 MySQL 组合唯一键时遇到问题。

它由 URL、整数值和日期字段组成。

但是当我尝试插入行时,出现异常:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://cars.auto.ru/cars/used/sale/16152870-c13f1.html-2012-02-1' for key 'one_a_day_idx'

如您所见,组合索引被截断了 64 个字符,因此它不再是唯一的(我每天从外部源检索数据一次)

但最令人困惑的是记录被插入,尽管抛出了关于约束违反的异常

有一个类似的问题此处,但唯一的建议是使用 SHOW CREATE TABLE 找出索引的实际长度。

Show create table 显示了这一点:

| auto_ru_sale | CREATE TABLE `auto_ru_sale` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `template` int(11) NOT NULL,
  `region` varchar(128) NOT NULL,
  `URI` varchar(128) NOT NULL,
  `subType` varchar(128) NOT NULL,
  `cost` int(11) NOT NULL,
  `productionYear` int(11) NOT NULL,
  `engineVolume` int(11) NOT NULL,
  `transmitionType` varchar(1) NOT NULL,
  `run` int(11) NOT NULL,
  `evaluationDate` date NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `one_a_day_idx` (`template`,`URI`,`evaluationDate`),
  KEY `prodyear_idx` (`productionYear`),
  KEY `evdate_idx` (`evaluationDate`),
  CONSTRAINT `auto_ru_sale_ibfk_1` FOREIGN KEY (`template`) REFERENCES `auto_ru_
datatemplate` (`ID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=127012 DEFAULT CHARSET=utf8 |

因此,我没有看到索引长度有任何限制。

非常感谢大家可以就这个问题提供一些帮助。

I'm having problems with MySQL composed unique key.

It consists of URL, integer value and date field.

But when I try to insert row, I get an exception:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://cars.auto.ru/cars/used/sale/16152870-c13f1.html-2012-02-1' for key 'one_a_day_idx'

As you can see, composed index was truncated by 64 characters, and because of this it is not an unique any more (I'm retrieving data from external source once a day)

But the most confusing that the record was inserted, though an exception about constraint violation was thrown

There was a similar question here, but the only advise was to use SHOW CREATE TABLE to find out the actual length of the index.

Show create tables shows this:

| auto_ru_sale | CREATE TABLE `auto_ru_sale` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `template` int(11) NOT NULL,
  `region` varchar(128) NOT NULL,
  `URI` varchar(128) NOT NULL,
  `subType` varchar(128) NOT NULL,
  `cost` int(11) NOT NULL,
  `productionYear` int(11) NOT NULL,
  `engineVolume` int(11) NOT NULL,
  `transmitionType` varchar(1) NOT NULL,
  `run` int(11) NOT NULL,
  `evaluationDate` date NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `one_a_day_idx` (`template`,`URI`,`evaluationDate`),
  KEY `prodyear_idx` (`productionYear`),
  KEY `evdate_idx` (`evaluationDate`),
  CONSTRAINT `auto_ru_sale_ibfk_1` FOREIGN KEY (`template`) REFERENCES `auto_ru_
datatemplate` (`ID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=127012 DEFAULT CHARSET=utf8 |

So, I don't see any limitations to index length.

Many thanks to everyone, who can give some help about this issue.

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

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

发布评论

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

评论(2

拍不死你 2025-01-14 13:30:30

我今天遇到了同样的问题。

我的问题是,唯一索引实际上超过 64 个字符,但错误消息仅显示前 64 个字符。
我认为这里也是同样的情况。

所以我遇到的是一个实际的重复键,这是由某些错误的日期格式引起的。

I experienced the same problem today.

The problem in my case was that the unique index actually was longer than 64 characters, but the error message only shows the first 64 characters.
I think the it is the same case here.

So what I encountered was an actual duplicate key, which was caused by some wrong date-format.

完美的未来在梦里 2025-01-14 13:30:30

我没有回答您的确切问题,但有一个建议可以在改进数据库时间的同时修复它。

创建一个表,即 URI (URI_id INT UNSIGNED PK AI, URI VARCHAR(128) NOT NULL, UNIQUE KEY (URI)) 并将 auto_ru_sale 更改为存储 uri_id 而不是 URI

这将意味着该键成为 INT、INT、DATE 的键会更有效率。由于重复项减少,您的表大小将会减小。总而言之,正常化是有益的。

我还建议将同样的方法应用于“区域”字段,因为我假设其中也有很多重复的数据。

I haven't an answer to your exact question however a suggestion which should fix it while improving database times.

Create a table i.e. URIs (URI_id INT UNSIGNED PK AI, URI VARCHAR(128) NOT NULL, UNIQUE KEY (URI)) and change auto_ru_sale to store uri_id instead of URI

This will mean that key becomes a key of INT,INT,DATE which will be more efficient. Your table size will reduce due to less duplicates. All in all normalisation is beneficial.

I would also advise the same applied to the "region" field as I would assume that has a lot of duplicate data in also.

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