Search code examples
mysqlauto-incrementmysql-5.6

auto increment holes in insert statement mysql 5.6


I am trying to move a table which contains billions of rows to a new directory in MySQL 5.6. I am trying to copy table1 to table2 and there by droping table1 and then renaming table2 to table1.

    CREATE TABLE `table2` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `col1` int(11) DEFAULT NULL,
  `col2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_col1_col2` (`col1`,`col2`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 DATA DIRECTORY='/mysql_data/';

I am using the below procedure to do the copy.

DROP PROCEDURE IF EXISTS copytables;
CREATE PROCEDURE `copytables`()
begin
DECLARE v_id INT(11) unsigned  default 0;
declare maxid int(11) unsigned  default 0;
select max(id) into maxid from table1;
while v_id < maxid  do
insert into table2(col1,col2)
select fbpost_id,fbuser_id from table1 where id >= v_id and id <v_id+100000 ;
set v_id=v_id+100000;
select v_id;
select max(id) into maxid from table1;
select maxid;
end while;
end;

But now I am getting gaps in id column after every batch of 100000 in table2 (after the id 199999 next id is 262141). Table1 is not containing any gaps in id column.


Solution

  • Ask Google: https://www.google.com/search?q=auto_increment+mysql+gaps+innodb The first result explains this issue.

    Generally, you need to be able to tell SO people what you have tried so far and why it isn't working. In this case, this is just a feature/characteristic of the InnoDB engine that lets it operate quickly at high volumes.