ON DUPLICATE KEY UPDATE 的自动增量太多

2023-07-17数据库问题
2

本文介绍了ON DUPLICATE KEY UPDATE 的自动增量太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个带列的基本表格:

I have a basic table with columns:

  • id(主要与 AI)
  • 名称(唯一)

如果唯一列不存在,则插入该行,否则更新该行....

If the unique column doesn't exist, INSERT the row, otherwise UPDATE the row....

INSERT INTO pages (name, etc)
VALUES
  'bob',
  'randomness'
ON DUPLICATE KEY UPDATE
 name = VALUES(name),
 etc = VALUES(etc)

问题在于,如果它执行 UPDATE,则 id 列上的 auto_increment 值会上升.因此,如果执行了一大堆 UPDATES,则 id auto_increment 就会突破.

The problem is that if it performs an UPDATE, the auto_increment value on the id column goes up. So if a whole bunch of UPDATES are performed, the id auto_increment goes through the roof.

显然这是一个错误:http://bugs.mysql.com/bug.php?id=28781

...但我在共享主机上的 mySQL 5.5.8 上使用 InnoDB.

...but I'm using InnoDB on mySQL 5.5.8 on shared hosting.

其他人在几年前遇到了没有解决方案的问题:防止 MYSQL 重复插入的自动增量和为什么插入失败时 MySQL 自动增量会增加?

Other people having issues with no solution years ago: prevent autoincrement on MYSQL duplicate insert and Why does MySQL autoincrement increase on failed inserts?

关于修复的想法?我是否以某种方式错误地构建了数据库?

Ideas on a fix? Have I maybe structured the database incorrectly somehow?

******EDIT****:似乎在 my.ini 文件中添加 innodb_autoinc_lock_mode = 0 可以解决问题,但我有哪些共享托管选项?

******EDIT****: It appears adding innodb_autoinc_lock_mode = 0 to your my.ini file fixes the problem but what options do I have for shared hosting?

******EDIT 2******:好的,我认为我唯一的选择是更改为 MyISAM 作为存储引擎.作为一个大型 mySQL 新手,我希望这不会引起很多问题.是吗?

******EDIT 2******: OK, I think my only option is to change to MyISAM as the storage engine. Being a mega mySQL newbie, I hope that doesn't cause many issues. Yeah?

推荐答案

我认为没有办法绕过 INSERT ... ON DUPLICTE KEY UPDATE 的这种行为.

I don't think there is a way to bypass this behaviour of INSERT ... ON DUPLICTE KEY UPDATE.

然而,您可以将两个语句,一个 UPDATE 和一个 INSERT,放在一个 交易:

You can however put two statements, one UPDATE and one INSERT, in one transaction:

START TRANSACTION ;

UPDATE pages
SET etc = 'randomness'
WHERE name = 'bob' ;

INSERT INTO pages (name, etc)
SELECT 
      'bob' AS name
    , 'randomness' AS etc 
FROM dual 
WHERE NOT EXISTS
      ( SELECT *
        FROM pages p
        WHERE p.name = 'bob'
      ) ;

COMMIT ;

这篇关于ON DUPLICATE KEY UPDATE 的自动增量太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14