Magento 2 Bestsellers Report: Unnecessary Full History Recomputation Slows Down Cron Jobs

Magento 2 Bestsellers Report: Unnecessary Full History Recomputation Slows Down Cron Jobs

In the complex world of e-commerce, efficient data processing is paramount, especially for large Magento 2 stores with extensive sales histories. A recent GitHub issue (#41844) has brought to light a significant performance bottleneck within Magento 2's core bestsellers report aggregation, specifically impacting the cron job responsible for refreshing sales statistics.

The Core Performance Drain: Full History Recomputation

The issue, reported by lbajsarowicz and confirmed on Magento 2.4-develop, details how the aggregate_sales_report_bestsellers_data cron job, intended to refresh daily bestsellers data, inadvertently triggers a full recomputation of product rating positions (rating_pos) across the entire sales history. This happens despite the cron job only needing to update a specific, recent period.

The problem lies in the internal workings of the Magento\Sales\Model\ResourceModel\Report\Bestsellers::aggregate($from, $to) method. While it correctly processes the specified date range, it subsequently calls _updateRatingPos() without any range limitation. This leads to Reports\Model\ResourceModel\Helper::updateReportRatingPos() clearing and rebuilding the rating_pos for every period in the daily, monthly, and yearly aggregated tables, regardless of whether they were affected by the current refresh.

// The problematic call sequence:
Magento\Sales\Model\ResourceModel\Report\Bestsellers::aggregate($from, $to)
  -> calls _updateRatingPos() (without range)
    -> which calls Reports\Model\ResourceModel\Helper::updateReportRatingPos() (for full history)

Impact on Magento Stores and Migrations

For stores with several years of order history—for instance, 730 days of daily data for 300 products across 2 stores, totaling over 438,000 rows in sales_bestsellers_aggregated_daily—this bug translates into a massive, unnecessary workload on every cron run. The reporter observed the rating position step taking nearly 3 seconds and rewriting almost 437,400 daily, 14,400 monthly, and 1,200 yearly rows outside the refreshed periods. If limited to the refreshed periods, this operation would take a mere 0.18 seconds and rewrite no unaffected rows.

This performance hit is particularly critical for:

  • Large-scale Magento deployments: Stores with extensive historical data will experience significantly longer cron execution times and increased server load.
  • Adobe Commerce users: As a core Magento component, this affects both Open Source and Adobe Commerce editions.
  • E-commerce migrations: When migrating to Magento 2, especially with full historical data, this issue can immediately manifest as a performance bottleneck, impacting post-migration stability and operational efficiency.

The cost of this operation grows linearly with the amount of historical data, meaning long-running stores are penalized more severely with each cron run. This can lead to cron jobs timing out, resource exhaustion, and overall system instability.

Community Validation and Next Steps

The issue was quickly confirmed by `engcom-Bravo` on a vanilla Magento 2.4-develop instance, validating the reproducibility and severity of the bug. This confirmation is a crucial step in the Magento contribution workflow, indicating that the issue is officially recognized and will be addressed by the core team.

While the comments do not yet provide a direct solution, the issue body mentions related efforts like ACP2E-4559 and PR #36537, suggesting that the Magento team is aware of and working on improvements in this area. For merchants and developers experiencing slow bestsellers report processing, monitoring this GitHub issue for updates and potential fixes is highly recommended. In severe cases, a temporary custom override or patch might be considered to limit the scope of the _updateRatingPos() call.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools