insert into on duplicate key update

question

There is a table, the table creation statement is as follows:

CREATE TABLE `tbl_host` (
  `id` bigint(64) NOT NULL AUTO_INCREMENT,
  `ip` varchar(255) NOT NULL DEFAULT '',
  `host_name` varchar(2555) NOT NULL DEFAULT '',
`timestamp` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_ip` (`ip`)
) ENGINE=InnoDB AUTO_INCREMENT=84689426 DEFAULT CHARSET=utf8;

in,

  • id is the primary key, auto-incrementing field
  • The ip field is the unique key

Use sql statements when inserting and updating tables:

insert into tbl_host(ip, host_name, timestamp) values ('%s', '%s', '%d') on duplicate key update host_name='%s', timestamp='%d'

When updating table records frequently, pass

mysql> show create table tbl_host

Found that AUTO_INCREMENT is increasing. Begin to wonder if there is a problem with the sql statement used.

The mysql official website gives instructions:

With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.

Specifically for INSERT ON DUPLICATE KEY UPDATE, even if the update is finally executed, the auto-increment ID will increase.

If the id continues to grow, will it go out of bounds? For the int64 data type, 10w records are updated every minute, which will not be consumed in 100 million years. Therefore, for out-of-bounds problems, you can use it with confidence.

suggestion

When the operation on the table is less insert and requires a large number of updates, it is not recommended to use insert into on duplicate key update. So how to deal with the situation?

  • You can select first, update if found, otherwise insert.
  • Or you can update first, and then insert if it fails. This can greatly improve efficiency.

refer to

mysql official website
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

statckoverflow
https://stackoverflow.com/questions/23516958/on-duplicate-key-auto-increment-issue-mysql

What happens if mysql's AUTO_INCREMENT reaches the maximum value?
https://blog.csdn.net/stpeace/article/details/78066262

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170098&siteId=291194637