1

I'm using Mysql 5.6.46.

And the final sql execute in my code is REPLACE ... .

I'm using multi thread way to batch insert data into my db table.

The question is the AUTO_INCREMENT is less than my table max ID, and then I can not write data into this table for it always raise error Duplicate entry '31245418' for key 'PRIMARY'

SELECT AUTO_INCREMENT 
FROM information_schema.tables WHERE table_schema='db' AND table_name='table';
# 31245419

SELECT id FROM spend_daily_level ORDER BY id DESC LIMIT 1
# 31247125

How can I avoid this problem? Thanks

I will provide more detail info if it's necessary

Update more info

CREATE TABLE `spend_daily_level` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `active` tinyint(1) NOT NULL ,
  `created_time` datetime(6) NOT NULL ,
  `updated_time` datetime(6) NOT NULL ,
  `date` date NOT NULL ,
  `system_value` decimal(16,2) NOT NULL ,
  `checked_value` decimal(16,2) NOT NULL ,
  `account_id` int(11) NOT NULL ,
  `sale_leader_id` int(11) DEFAULT NULL ,
  `account_status` tinyint(3) DEFAULT '1' ,
  PRIMARY KEY (`id`),
  UNIQUE KEY `spend_daily_level_date_account_id_f38b1186_uniq` (`date`,`account_id`),
  KEY `spend_daily_level_account_id_f6df4f99_fk_account_id` (`account_id`),
  KEY `sale_leader_id` (`sale_leader_id`),
  KEY `date_active` (`active`,`date`),
  CONSTRAINT `spend_daily_level_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`),
  CONSTRAINT `spend_daily_level_ibfk_2` FOREIGN KEY (`sale_leader_id`) REFERENCES `sale_leader` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31245419 DEFAULT CHARSET=utf8 COMMENT='daily_spend'

jia Jimmy
  • 1,693
  • 2
  • 18
  • 38
  • 1
    I think the bigger problem is the source of the `ID` values. Can you include the insert which is failing? – Tim Biegeleisen Nov 19 '21 at 04:04
  • This should not be possible, unless `id` is not the auto-increment column of this table. Can you run `SHOW CREATE TABLE spend_daily_level` and share the result? – Bill Karwin Nov 19 '21 at 04:18
  • As a one-time fix you could change the AUTOINCREMENT value for the table. See [this question](https://stackoverflow.com/q/970597/14853083) for suggestions on how to do that. Note that this can trigger a rebuild of a table, and that can take a long time if the table is large. The linked question has answers that cover that possibility. You will need to find out where these rogue values are coming from. There's nothing here to indicate how that's happening. – Tangentially Perpendicular Nov 19 '21 at 04:22
  • @Bill It's possible to INSERT a row with a value in an AUTOINCREMENT column that's larger than the current AUTOINCREMENT value. I suspect that's what's happening here. – Tangentially Perpendicular Nov 19 '21 at 04:23
  • @TangentiallyPerpendicular At least with InnoDB, that always causes the auto-increment value you see in the information_schema to bump up to the value you just inserted. Try it. – Bill Karwin Nov 19 '21 at 04:26
  • @Bill I learn something new every day! – Tangentially Perpendicular Nov 19 '21 at 04:28
  • If AI column value in I_S.TABLES is less than max. value in this AI column in the table itselt then this means that some query have inserted or updated a row with this column explicit value specifying. – Akina Nov 19 '21 at 04:49
  • @BillKarwin Thanks for reply, i've updated more info – jia Jimmy Nov 19 '21 at 04:49
  • Okay I see that `id` is in fact the auto-increment column, and the table shows 31245419 as the next value. I have never seen it possible that the table can show a next AI value less than the max value in that column. The reverse is possible — the table may show a next AI value greater than the max value that is stored. For instance if you delete some rows or if you use ALTER TABLE to advance the next AI option for the table. I don't know how to explain what you are seeing, unless you're querying the wrong table. – Bill Karwin Nov 19 '21 at 05:52
  • @BillKarwin I'm using something like `INSERT IGNORE INTO ...` or `REPLACE ...` sql to batch insert some data in multi thread way. Is it related to this operation? – jia Jimmy Nov 19 '21 at 06:00
  • As far as I know, the table's AUTO_INCREMENT option will never be less than the current max value in the AI column. It doesn't matter how you cause that value to get into the column. You can `INSERT` a value explicitly, or else let the AI generate a new value, or even `UPDATE` to change the value on an existing row. Any way you do it, it should push the table's AUTO_INCREMENT option up to be at least 1 greater than the current max value. – Bill Karwin Nov 19 '21 at 06:03
  • @BillKarwin Yeah, that's weird. Is it related to `Master-slave database` mode as I use a `clound db service` – jia Jimmy Nov 19 '21 at 06:09
  • @BillKarwin [Replace into causes master/slave have different auto_increment offset values](https://bugs.mysql.com/bug.php?id=87861) – jia Jimmy Nov 19 '21 at 09:18
  • That bug describes the opposite of what you report. The bug is that the AUTO_INCREMENT table option advances by one, while the value in the table does not. This is known behavior. You can also get an AUTO_INCREMENT greater than the max value in the column if you delete rows, or if you manually ALTER TABLE ... AUTO_INCREMENT=. But you are reporting the other way: the max value in the column is greater than the table's AUTO_INCREMENT, which I have never seen happen. – Bill Karwin Nov 19 '21 at 14:31
  • @BillKarwin Thanks for your patience. I've googled a chinese technical article [Depth analysis auto-increment self-acquired "Duliplicate Key" problem](https://mp.weixin.qq.com/s/YjPOayOFwCPwH084SG2Ggw) which said that might be a `Master-slave` exchange issue but I cannot fully understand for my lack experience of `DB operation` – jia Jimmy Nov 22 '21 at 02:44

0 Answers0