Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N°7968 - ⚡️ Add faster way to rename column on the same table #676

Merged

Conversation

Hipska
Copy link
Contributor

@Hipska Hipska commented Nov 6, 2024

Base information

Question Answer
Related to a SourceForge thead / Another PR / Combodo ticket? N/A
Type of change? Enhancement

Objective

The process of moving data from one column to another could be simplified a lot when both are on the same table.

Proposed solution

Execute a RENAME COLUMN SQL statement instead of the following steps:

  1. Create new column
  2. Copy data from old to new column
  3. Delete old column

Checklist before requesting a review

  • I have performed a self-review of my code
  • I have tested all changes I made on an iTop instance
  • I have added a unit test, otherwise I have explained why I couldn't
  • Is the PR clear and detailed enough so anyone can understand digging in the code?

@Hipska Hipska changed the title Add faster way to rename column on the same table ⚡️ Add faster way to rename column on the same table Nov 6, 2024
Copy link
Contributor

@Molkobain Molkobain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful, this is compatible with MySQL 8.0+, but not MySQL 5.7 which is still supported by iTop. You should stick to the CHANGE synthax:

Database Version CHANGE Compatibility RENAME COLUMN Compatibility
MySQL < 8.0 Yes No
MySQL 8.0+ Yes Yes
MariaDB < 10.5.2 Yes No
MariaDB 10.5.2+ Yes Yes

@Hipska
Copy link
Contributor Author

Hipska commented Nov 6, 2024

Oh, that's probably the reason I didn't create the PR back then.

Downside with CHANGE is that it needs the column definition, but there's a method for that, so fixed in 318a7e9.

@Hipska Hipska requested a review from Molkobain November 6, 2024 13:05
Copy link
Contributor

@Molkobain Molkobain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMDBSource::GetfIeldSpec() can return false, but I rather have an explicit error than hiding it. Good job.

@steffunky
Copy link
Member

We already have some tests for this method in tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php, adding one testing this use case could be nice as @Molkobain already caught a regression in this method once
If you don't have time I'll try to add one sometime soon

@Hipska
Copy link
Contributor Author

Hipska commented Nov 6, 2024

@Molkobain actually, the same method is doing the same without any checks:

$sSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);

It will just never occur to return false because of the checks before.

@Hipska Hipska changed the base branch from develop to support/3.2.0 November 7, 2024 15:47
@Hipska Hipska changed the base branch from support/3.2.0 to develop November 7, 2024 15:48
@Hipska Hipska requested a review from Molkobain November 7, 2024 15:55
Copy link
Member

@steffunky steffunky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well on MariaDB 11.3.2 👍

@Hipska
Copy link
Contributor Author

Hipska commented Nov 7, 2024

Funny, as that version isn't even supported by Combodo.

@steffunky steffunky added API Performance Setup Related to the setup process (install / upgrade) labels Nov 8, 2024
@jf-cbd jf-cbd requested a review from rquetiez November 8, 2024 09:55
@rquetiez
Copy link
Contributor

rquetiez commented Nov 8, 2024

To take all the benefits out of this proposal, the table alteration algorithm must be at least inplace.

I've just conducted a quick study to check which DDL algorithm is used, and make sure that the syntax ALTER TABLE table CHANGE column... would not imply the infamous COPY algorithm.

Conclusion

The proposed implementation will be quick in any versions supported by iTop, excepted for the rare cases when the COPY algorithm is still used (e.g. table created with mariaDB < 10.3 and never rebuilt since the upgrade of the server).

Methodology

As an alternative to measuring the time spent on executing the alter table statement on a "huge" table, one can specify algorithm=inplace, so that the alter table statement would fail if the engine is not able to execute the INPLACE algorithm (or a quicker algorithm).

I've also tested with lock=none to document wether the operation involves locking or not (See Online DDL Performance and Concurrency)

alter table organization change code new_code varchar(255) default '' null, algorithm=inplace, lock=none;
alter table organization change code new_code varchar(255) default '' null, algorithm=instant;

Results

Database Version algorithm=inplace, lock=none algorithm=instant
MySQL 5.7 Yes n/a
MySQL 8.3 Yes Yes
MariaDB 10.3 Yes Yes

@jf-cbd jf-cbd merged commit 7e1b177 into Combodo:develop Nov 14, 2024
@jf-cbd
Copy link
Contributor

jf-cbd commented Nov 14, 2024

Thanks a lot @Hipska for the contribution !

@Hipska Hipska deleted the feature/moduleinstaller/rename_column branch November 14, 2024 13:26
@jf-cbd jf-cbd changed the title ⚡️ Add faster way to rename column on the same table N°7968 - ⚡️ Add faster way to rename column on the same table Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Performance Setup Related to the setup process (install / upgrade)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants