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

magento/magento2#: Add a new index for cron_schedule table #27391

Merged

Conversation

atwixfirster
Copy link
Contributor

@atwixfirster atwixfirster commented Mar 22, 2020

Description (*)

Magento\Cron\Model\ResourceModel\Schedule::trySetJobStatusAtomic() executes an UPDATE SQL with an additional status parameter:

 UPDATE `cron_schedule` SET `status` = ? WHERE (schedule_id = ?) AND (status = ?) 

UPDATE is a hard query and New Relic report about this:

27391_2

27391

PR adds an additional index based on

  • schedule_id
  • status

fields.

Related Pull Requests

Fixed Issues (if relevant)

  1. magento/magento2#<issue_number>: Issue title

Manual testing scenarios (*)

  1. ...
  2. ...

Questions or comments

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)
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] magento/magento2#: Add a new index for cron_schedule table #29601: magento/magento2#: Add a new index for cron_schedule table

@m2-assistant
Copy link

m2-assistant bot commented Mar 22, 2020

Hi @atwixfirster. Thank you for your contribution
Here is 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

For more details, please, review the Magento Contributor Guide documentation.

@magento-engcom-team magento-engcom-team added Component: Cron Release Line: 2.4 Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels Mar 22, 2020
@kandy
Copy link
Contributor

kandy commented Mar 22, 2020

Query that you show use "where" condition on primary keys and so should run with a millisecond performance

See

mysql> UPDATE `cron_schedule` SET `status` = 'run' WHERE (schedule_id = 1153)  AND (status = 'pending');
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> DESC UPDATE `cron_schedule` SET `status` = 'pending' WHERE (schedule_id = 1)  AND (status = 'pending');
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table         | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra       |
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
|  1 | UPDATE      | cron_schedule | NULL       | range | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | Using where |
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set (0.00 sec)

Copy link
Contributor

@kandy kandy left a comment

Choose a reason for hiding this comment

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

Can you add database version and explain for this query ?

@atwixfirster
Copy link
Contributor Author

atwixfirster commented Mar 22, 2020

Hi, @kandy !

Can you add database version and explain for this query ?

Magento Cloud staging environment.

DB version = 10.0.38-MariaDB-1~jessie-wsrep

Table cron_schedule contains ~15 000 records.

@atwixfirster
Copy link
Contributor Author

UPDATE here:

Today, I've analyzed New Relic reports and detected that a new index did not help to reduce UPDATE execution time.

In the same time, I've changed a logic of Magento\Cron\Model\ResourceModel\Schedule::trySetJobStatusAtomic():

public function trySetJobStatusAtomic($scheduleId, $newStatus, $currentStatus)
    {
        $connection = $this->getConnection();
        $select = $connection->select()
            ->from(
                $this->getTable('cron_schedule'),
                [
                    'status'
                ]
            )->where(
                'schedule_id = ?', $scheduleId
            );
        $resultRow = $connection->fetchRow($select);

        $result = false;
        if ($resultRow['status'] != $newStatus) {
            $result = $connection->update(
                $this->getTable('cron_schedule'),
                ['status' => $newStatus],
                ['schedule_id = ?' => $scheduleId]
            );
        }

        return (bool)$result;
    }

The mentioned approach helped to reduce UPDATE queries calls.

kandy
kandy previously approved these changes Aug 5, 2020
@engcom-Alfa
Copy link
Contributor

Hi @kandy . Could you put an appropriate label for test coverage?
Thanks!

Copy link
Contributor

@engcom-Alfa engcom-Alfa left a comment

Choose a reason for hiding this comment

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

Hi @atwixfirster . We have some problems with checkout to your branch. Could you update it?
Thanks!

@engcom-Charlie
Copy link
Contributor

Hi, @sidolov can you please review this PR?
Thank you.

@sidolov sidolov added Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests labels Sep 8, 2020
@magento-engcom-team
Copy link
Contributor

Hi @sidolov, thank you for the review.
ENGCOM-8164 has been created to process this Pull Request

@engcom-Alfa
Copy link
Contributor

✔️ QA Passed

A new index for cron_schedule table was successfully added and everything works as expected.

2020-09-09_13-45

@engcom-Alfa engcom-Alfa added the QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope label Sep 9, 2020
@hostep
Copy link
Contributor

hostep commented Sep 9, 2020

Before this gets merged, can somebody confirm that this won't conflict with #28007 ?

@kandy
Copy link
Contributor

kandy commented Sep 9, 2020

I can confirm that it will conflict as it duplicate to #28007?

@magento-engcom-team magento-engcom-team merged commit 707dc2e into magento:2.4-develop Sep 10, 2020
@m2-assistant
Copy link

m2-assistant bot commented Sep 10, 2020

Hi @atwixfirster, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Sep 10, 2020

@sidolov @gabrieldagama i believe this should be reverted in favor to #28007 that does the same and was approved more than 2 weeks ago

driskell added a commit to driskell/magento2 that referenced this pull request Sep 21, 2020
Status field is no longer used in that query after this PR
driskell added a commit to driskell/magento2 that referenced this pull request Sep 21, 2020
Status field is no longer used in the query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests Component: Cron Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Priority: P3 May be fixed according to the position in the backlog. Progress: accept QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Issue] magento/magento2#: Add a new index for cron_schedule table
8 participants