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

Very slow catalogrule_product index even when no catalog rule exists #34784

Closed
5 tasks
dudzio12 opened this issue Dec 9, 2021 · 8 comments · Fixed by #37889
Closed
5 tasks

Very slow catalogrule_product index even when no catalog rule exists #34784

dudzio12 opened this issue Dec 9, 2021 · 8 comments · Fixed by #37889
Assignees
Labels
Area: Catalog Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: done Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@dudzio12
Copy link
Member

dudzio12 commented Dec 9, 2021

On a Magento 2.4.3 instance:

  • I don't have any catalog rule
  • I have ~100k products, mostly configurable
  • I have 4 websites
  • Have almost all my indexes setup on scheduled mode

Preconditions (*)

Steps to reproduce (*)

  1. Add some ids (or update some product attributes) into catalogrule_product_cl table.
  2. Run indexer_update_all_views CRON job.

Expected result (*)

  1. Product ids quantity should not have that big impact in index processing time.
  2. The index process should end in a reasonable time.

Actual result (*)

  1. Product ids quantity is brutally impacting execution time.
  2. Index process should end in a moment, when no catalog rule is set.

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Dec 9, 2021

Hi @dudzio12. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues 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 issues 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.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

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

@dudzio12
Copy link
Member Author

dudzio12 commented Dec 9, 2021

protected function doReindexByIds($ids)
    {
        $this->cleanProductIndex($ids);

        /** @var Rule[] $activeRules */
        $activeRules = $this->getActiveRules()->getItems();

        foreach ($activeRules as $rule) {
            $rule->setProductsFilter($ids);
            $this->reindexRuleProduct->execute($rule, $this->batchCount);
        }

        foreach ($ids as $productId) {
            $this->cleanProductPriceIndex([$productId]);
            
            ## I've added `if` statement as seen below and there is no need to reindex prices
            ## if only  above cleaning cleans it correctly and no activeRule exists.
            ## That only sppeds up indexation process more that 10 times
            if (count($activeRules) > 0) {
                $this->reindexRuleProductPrice->execute($this->batchCount, $productId);
            }

        }

        $this->reindexRuleGroupWebsite->execute();
    }

@dudzio12
Copy link
Member Author

dudzio12 commented Dec 9, 2021

I'll prepare PR and run some tests with that change implemented. It won't fix non optimal index, but will lock some unnecessary code execution.

@m2-assistant
Copy link

m2-assistant bot commented Dec 13, 2021

Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Alfa engcom-Alfa added the Priority: P3 May be fixed according to the position in the backlog. label Dec 13, 2021
@m2-community-project m2-community-project bot added this to Pull Request In Progress in Low Priority Backlog Dec 13, 2021
@m2-community-project m2-community-project bot removed this from Ready for Confirmation in Issue Confirmation and Triage Board Dec 13, 2021
@engcom-Hotel engcom-Hotel removed their assignment Dec 14, 2021
@dudzio12
Copy link
Member Author

Fixed by #34785

@m2-community-project m2-community-project bot moved this from Pull Request In Progress to Ready for Development in Low Priority Backlog Feb 16, 2022
@aligent-lturner
Copy link
Contributor

The performance of this indexer appears to be incredibly slow, as it reindexes products one by one, instead of in batches.
I've taken a quick look at the code involved, and I can't really see a good reason why this has been done.

The select builder used is here -

public function build(int $websiteId, ?int $productId = null, bool $useAdditionalTable = false)
{
$connection = $this->resource->getConnection();
$indexTable = $this->resource->getTableName('catalogrule_product');
if ($useAdditionalTable) {
$indexTable = $this->resource->getTableName(
$this->tableSwapper->getWorkingTableName('catalogrule_product')
);
}
/**
* Sort order is important
* It used for check stop price rule condition.
* website_id customer_group_id product_id sort_order
* 1 1 1 0
* 1 1 1 1
* 1 1 1 2
* if row with sort order 1 will have stop flag we should exclude
* all next rows for same product id from price calculation
*/
$select = $connection->select()->from(
['rp' => $indexTable]
)->order(
['rp.website_id', 'rp.customer_group_id', 'rp.product_id', 'rp.sort_order', 'rp.rule_id']
);
if ($productId) {
$select->where('rp.product_id=?', $productId);
}

A single product id is passed in, and used to restrict the query to that product, but the query would still work if multiple products were passed in and an IN clause was used instead.

Similarly, the place that calls and then uses the query built by RuleProductsSelectBuilder is here -

$productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $productId, $useAdditionalTable);
$dayPrices = [];
$stopFlags = [];
$prevKey = null;
$storeGroup = $this->storeManager->getGroup($website->getDefaultGroupId());
$dateInterval = $this->useWebsiteTimezone
? $this->getDateInterval((int)$storeGroup->getDefaultStoreId())
: $this->getDateInterval(Store::DEFAULT_STORE_ID);
while ($ruleData = $productsStmt->fetch()) {
$ruleProductId = $ruleData['product_id'];
$productKey = $ruleProductId .
'_' .
$ruleData['website_id'] .
'_' .
$ruleData['customer_group_id'];
if ($prevKey && $prevKey != $productKey) {
$stopFlags = [];
if (count($dayPrices) > $batchCount) {
$this->pricesPersistor->execute($dayPrices, $useAdditionalTable);
$dayPrices = [];
}
}

This creates a key value using the product id coming from the data the query returns, so it too would still work with multiple products.

Maybe I'm missing something here, but I think that changing these functions to take in an array of product ids instead of a single one (and then batching appropriately in IndexBuilder) would dramatically reduce the amount of time required.

@m2-community-project m2-community-project bot moved this from Ready for Development to Pull Request In Progress in Low Priority Backlog Aug 17, 2023
@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Catalog and removed Issue: ready for confirmation labels Jan 23, 2024
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.adobe.com/browse/AC-10911 is successfully created for this GitHub issue.

Copy link

m2-assistant bot commented Jan 23, 2024

✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Catalog Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: done Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
5 participants