Skip to content

Conversation

michalbiarda
Copy link
Contributor

@michalbiarda michalbiarda commented Aug 25, 2022

Description (*)

Magento's MySQL adapter uses CREATE TEMPORARY TABLE ... LIKE command in \Magento\Framework\DB\Adapter\Pdo\Mysql::createTemporaryTableLike.

Newer versions of MySQL (8+) have problems with CREATE TEMPORARY TABLE ... LIKE, as you can read in the official documentation (https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html):

You cannot use CREATE TEMPORARY TABLE ... LIKE to create an empty table based on the definition of a table that resides in the mysql tablespace, InnoDB system tablespace (innodb_system), or a general tablespace.

The documentation suggests to use CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0 instead, but such command IS NOT an exact equivalent. It will create temporary table with the same fields, but will not create indexes. Without indexes performance of reindexing big amount of data is absurdly slow.

I suggest to build CREATE TEMPORARY TABLE without LIKE and without SELECT * FROM. Here's my idea:

Firstly use SHOW CREATE TABLE to get query for creation of source table (including indexes creation), and then modify it with PHP: change CREATE TABLE to CREATE TEMPORARY TABLE, add IF NOT EXISTS if necessary, remove TABLESPACE definition if it exists, and replace source table name with temporary table name.

Manual testing scenarios (*)

  1. Install Magento 2.4 on a server with MySQL 8.
  2. Apply sample data.
  3. Run bin/magento index:reindex.
  4. Check data on frontend.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Replaced CREATE TEMPORARY TABLE ... LIKEwith different approach #37926: Replaced CREATE TEMPORARY TABLE ... LIKEwith different approach
@m2-assistant
Copy link

m2-assistant bot commented Aug 25, 2022

Hi @michalbiarda. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@michalbiarda
Copy link
Contributor Author

@magento give me test instance

@magento-deployment-service
Copy link

Hi @michalbiarda. Thank you for your request. I'm working on Magento instance for you.

@michalbiarda
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

$temporaryTable = $this->quoteIdentifier($this->_getTableName($temporaryTableName));
$originTable = $this->quoteIdentifier($this->_getTableName($originTableName));
$sql = sprintf('CREATE TEMPORARY TABLE %s %s LIKE %s', $ifNotExistsSql, $temporaryTable, $originTable);
$originCreate = $this->fetchPairs(sprintf('SHOW CREATE TABLE %s', $originTable));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$originCreate = $this->fetchPairs(sprintf('SHOW CREATE TABLE %s', $originTable));
$originCreate = $this->fetchOne("SHOW CREATE TABLE {$originTable}");

You can use fetchOne to get result
Also sprintf is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kandy Unfortunately I can't use fetchOne because SHOW CREATE TABLE returns two fields. First is table name, the second is SQL query.

I like you suggestion about sprintf, though 👍.

@michalbiarda
Copy link
Contributor Author

@kandy I'm converting this PR to draft. I'll apply your suggestion about sprintf, but I will also fix one more thing reported by someone else in the meantime.

@michalbiarda michalbiarda marked this pull request as ready for review August 26, 2022 13:55
@michalbiarda
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Hotel engcom-Hotel added the Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. label Aug 26, 2022
@engcom-Hotel
Copy link
Contributor

@magento run Static Tests, Functional Tests EE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Hotel
Copy link
Contributor

Hello @michalbiarda,

Thanks for the contribution!

According to the below comment here, it seems that you are still working on it.

@kandy I'm converting this PR to draft. I'll apply your suggestion about sprintf, but I will also fix one more thing reported by someone else in the meantime.

Can you please confirm, if the PR is ready for review?

Thanks

@michalbiarda
Copy link
Contributor Author

Hello @engcom-Hotel,

After that comment I added something to my change, and marked this PR as ready for review 10 months ago 😅. I confirm it's still ready for review 😄.

@engcom-Lima engcom-Lima added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Jun 9, 2023
@engcom-Lima
Copy link
Contributor

@magento run Functional Tests EE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Hotel engcom-Hotel self-requested a review June 12, 2023 10:37
@engcom-Hotel
Copy link
Contributor

Hello @michalbiarda,

Thanks for the reply!

Hope you had a great vacation :)

After changing the innodb_file_per_table to OFF, the issue has been reproducible:

image

Hence we are proceeding further with this PR.

Thanks

@engcom-Hotel
Copy link
Contributor

✔️ QA Passed

Preconditions:

  1. SET innodb_file_per_table to OFF.

Manual testing scenario:

  • Install Magento 2.4 on a server with MySQL 8.
  • Generate data using the performance toolkit provided by Magento. Please refer to this link for this same.
  • Run bin/magento index:reindex.
  • Check data on front end.

Actual Result: ✔️

Reindex command should run properly without any errors and frontend is accessible with categories and products.

After: ✔️

image

Before: ✖️

image

Some functional tests are still failing hence moving this PR into extended testing.

Thanks

@engcom-Echo
Copy link
Contributor

@magento run Functional Tests EE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Echo
Copy link
Contributor

Functional Tests EE failures are different on last two run on same commit.
Run1
image

Run2
image

Hence moving to Merge In progress

@engcom-Charlie
Copy link
Contributor

@magento run Functional Tests EE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Hotel
Copy link
Contributor

@magento create issue

@engcom-Hotel
Copy link
Contributor

Moving back this PR to Merge in progress

@kennylawrence
Copy link

...sooooo..... what exactly is the solution? I currently have to remember to modify the mysql pdo adapter file (vendor/magento/framework/DB/Adapter/Pdo/Mysql.php) every time I update with any new Magento releases. This has been going on for at least four years, ever since MySQL v8. I host at MageMojo/Webscale, and at the moment the following links to their "official" fix for this at the following page whenever I forget and end up creating a new ticket because indexing fails: https://github.com/magemojo/m2-patches/wiki

Is there going to be a more permanent solution? Or is something wrong with the configuration at one of the largest Magento hosting environments (or possibly just my old config is out of date) ? From this issue thread, I wasn't able to grasp what exactly is the current status of the issue (wrong config? code?). Please can we have the final word on this?

@kennylawrence
Copy link

OK - I just realized that these kinds of fixes aren't added to security releases. I see in 2.4.7 the file has been updated according to the different approach mentioned above, while it does not exist in 2.4.6-p8. I will copy the relevant function over to my file. I am happy this is finally being put to rest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: accept Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
10 participants