Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL auto increment increasing by 10 (ClearDB & Node)

I have a simple table in ClearDB:

CREATE TABLE `users` (

`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`message` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)

) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

I'm using Node to insert data into the table via:

var post = {username: response.user.name, message: message.text};

           connection.query("INSERT INTO users SET ?", post, function(err, rows, fields) {

                 if (err) {
                       console.log('error: ', err);
                       throw err;
                 }

           });

However whenever I insert my id field increases by 10 rather than 1 (and it started off with 12:

id username message

12 test test

22 test test

32 test test

42 test test

Any idea why this is happening?

Thanks!

like image 713
Chris Olson Avatar asked Jan 11 '16 00:01

Chris Olson


People also ask

Can we change auto increment value in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.

What happens if MySQL auto increment reaches limit?

When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails. That is why it is advised to use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value required by us.

Can we have 2 auto increment in MySQL?

MySQL server already provides two auto increment variables: auto_increment_increment and auto_increment_offset, which can be used to generate different auto increment values on each member.

Why does auto increment jump by more than the number of rows inserted?

It could have multiple causes: Check if the auto_increment value on the table itself, has the next highest value. Mind that if you have transactions where you INSERT a row and rollback the transaction, that auto_increment value will be gone/skipped.


1 Answers

It is ClearDB's strategy. Here is the explanation from ClearDB's website.

You can't change this auto_increment step when you are using ClearDB.


This is the explanation from the link above.

ClearDB uses circular replication to provide master-master MySQL support. As such, certain things such as auto_increment keys (or sequences) must be configured in order for one master not to use the same key as the other, in all cases. We do this by configuring MySQL to skip certain keys, and by enforcing MySQL to use a specific offset for each key used. The reason why we use a value of 10 instead of 2 is for future development.

like image 84
Jeff Avatar answered Oct 06 '22 23:10

Jeff