I have a basic table with columns:
- id (primary with AI)
- name (unique)
- etc
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)
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.
Apparently it was a bug: http://bugs.mysql.com/bug.php?id=28781
...but I'm using InnoDB on mySQL 5.5.8 on shared hosting.
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****: 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******: 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?
I don't think there is a way to bypass this behaviour of
INSERT ... ON DUPLICTE KEY UPDATE
.You can however put two statements, one
UPDATE
and oneINSERT
, in one transaction:The on duplicate key functionality of MySQL is exactly the same as doing two separate queries, one to select, then one to either update the selected record, or insert a new record. Doing so programmatically is just as fast and will prevent this problem in the future as well as make your code more portable.