LP #1542291: pt-osc rebuild constraint can be faster since 5.6

Description

**Reported in Launchpad by DavidDucos last update 17-08-2016 17:54:24

Since 5.6, it is possible to use inplace algorithm for alter tables and this can be combined with FOREIGN_KEY_CHECKS=OFF.

This means that could be possible to rebuild the constraint in child tables pretty fast because it does not rebuild the table when you create the constraint.

I attached a patch for version 2.2.16 just for testing but I think that we need to change the logic because we need to determine mysql version.

This is my test case:

CREATE TABLE `parent` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `child` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fk_child` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `child_ibfk_1` (`fk_child`),
CONSTRAINT `parent_ibfk_1` FOREIGN KEY (`fk_child`) REFERENCES `parent` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

insert into parent values (); – Execute it several times

insert into child (fk_child) select * from parent;

insert into child (fk_child) select fk_child from child; – Execute it several times

  • This is for test that it takes times:
    alter table child add CONSTRAINT `parent_ibfk_2` FOREIGN KEY (`fk_child`) REFERENCES `parent` (`id`);

alter table child drop FOREIGN KEY `parent_ibfk_2`;

  • This is for test that it is really fast:
    set foreign_key_checks=off;

alter table child add CONSTRAINT `parent_ibfk_2` FOREIGN KEY (`fk_child`) REFERENCES `parent` (`id`);

alter table child drop FOREIGN KEY `parent_ibfk_2`;

set foreign_key_checks=on;

  • This is the error when you try to do it inplace with foreign_key_checks=ON :
    alter table child add CONSTRAINT `parent_ibfk_2` FOREIGN KEY (`fk_child`) REFERENCES `parent` (`id`), algorithm= inplace;
    ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Adding foreign keys needs foreign_key_checks=OFF. Try ALGORITHM=COPY.

I used this command to see if the inodes changed at os level:

! ls -li /var/lib/mysql/test/

Environment

None

Smart Checklist

Activity

Show:

Carlos Salguero August 2, 2021 at 5:28 PM

Thanks !

 

bob August 2, 2021 at 11:56 AM

You might want to set resolution to dup or something else than wontfix - that could frustrate casual readers..

Carlos Salguero July 30, 2021 at 10:38 PM

Thanks , you are right.

I am closing this ticket. 

bob July 30, 2021 at 7:38 PM

This is FOREIGN_KEY_CHECKS=OFF is already fixed in 3.3.1 along with , as I recall.

 

David Ducos July 30, 2021 at 3:26 PM

/

From my point of view, It is possible to use `--set-vars="FOREIGN_KEY_CHECKS=OFF"` with `--alter-foreign-keys-method=rebuild_constraints` to simulate this behavior, which is going to affect the whole process but should not be an issue.


I think that we need to consider to use `FOREIGN_KEY_CHECKS=OFF` by default and change the `auto` mode when the db version is >=5.6. Finally, change the documentation.

Duplicate

Details

Assignee

Reporter

Priority

Fix versions

Time tracking

1h logged

Smart Checklist

Created January 24, 2018 at 8:57 PM
Updated March 4, 2024 at 4:42 PM
Resolved August 2, 2021 at 5:28 PM