Magento 2 Performance Deep Dive: Unmasking the Report Statistics Refresh Bottleneck

Unveiling a Hidden Performance Drain in Magento 2 Report Aggregation

As e-commerce migration experts at Shopping Mover, we constantly monitor the Magento ecosystem for insights that can help merchants and developers optimize their stores. A recent GitHub issue (magento/magento2/#41837) sheds light on a significant performance bottleneck within Magento 2's report statistics refresh process, particularly affecting stores with large datasets. This issue, reported by lbajsarowicz and identified through production performance monitoring, highlights how a seemingly minor SQL query construction can lead to substantial performance degradation.

The Core Problem: Inefficient Report Aggregation with LIKE Chains

The heart of the issue lies in how Magento 2 aggregates report data, specifically for tables like sales_bestsellers_aggregated_daily. When the cron job aggregate_sales_report_bestsellers_data runs to refresh statistics, it interacts with the Magento\Sales\Model\ResourceModel\Report\Bestsellers::aggregate() method. For stores with extensive sales history – think millions of rows over several years – this process becomes incredibly slow.

The problem stems from the _makeConditionFromDateRangeSelect() method within Magento\Reports\Model\ResourceModel\Report\AbstractReport. Instead of building an efficient condition for date ranges, it constructs a series of LIKE 'YYYY-MM-DD' clauses, joined by OR, for each date within the refresh period. For example, refreshing two days might result in period LIKE '2026-11-09' OR period LIKE '2026-11-10'.

Technical Deep Dive: LIKE vs. IN - A Critical Performance Difference

The issue's author meticulously details the performance impact. When the database optimizer encounters a chain of LIKE conditions on a DATE column (even with an index), it often fails to utilize the index effectively. Instead, it resorts to a full table scan.

Consider the following problematic query structure:

INSERT ... SELECT ... FROM sales_bestsellers_aggregated_daily WHERE store_id <> 0 AND (period LIKE 'd1' OR period LIKE 'd2' OR ...) GROUP BY period, product_id

With a table of 1.2 million rows, refreshing just two days using this LIKE chain resulted in:

  • type=ALL: Indicating a full table scan.
  • Approximately 1.2 million rows examined.
  • A query execution time of 0.27 seconds per statement.

The author then demonstrates the dramatic improvement by simply changing the condition to an IN clause:

INSERT ... SELECT ... FROM sales_bestsellers_aggregated_daily WHERE store_id <> 0 AND (period IN ('d1', 'd2')) GROUP BY period, product_id

The same statement with period IN ('d1', 'd2') yielded:

  • type=range: Indicating efficient index usage.
  • Only 2402 rows examined.
  • A lightning-fast execution time of 0.002 seconds.

This stark difference underscores the importance of efficient SQL query construction, especially in high-load e-commerce environments like Magento 2. The issue also notes that the DELETE path for the same refresh already uses IN, suggesting an inconsistency in the codebase.

Impact on Magento 2 Stores and What It Means for You

This bug directly impacts the performance and stability of Magento 2 stores, particularly Adobe Commerce and Open Source instances with significant transaction volumes and long operational histories. Slow report aggregation leads to:

  • Extended Cron Job Execution Times: Report refresh cron jobs can run for hours, consuming valuable server resources.
  • Increased Database Load: Constant full table scans put unnecessary strain on the database server.
  • Delayed or Inaccurate Reports: Merchants might experience delays in accessing up-to-date sales, bestsellers, invoiced, refunded, shipping, tax, and sales rule reports.
  • Resource Contention: Long-running queries can block other database operations, impacting overall store responsiveness.

While the provided GitHub issue does not include community comments or an official resolution at this time, the detailed report by lbajsarowicz provides a clear diagnosis and an explicit solution: replacing the LIKE chain with an IN clause for date range conditions in affected report aggregation queries.

Actionable Insights for Developers and Merchants

For Magento 2 developers, this issue serves as a critical reminder to:

  • Scrutinize Database Queries: Always review the generated SQL queries, especially for cron jobs and reports, using tools like EXPLAIN to identify potential bottlenecks.
  • Prioritize Efficient Date Range Handling: Favor IN clauses or direct comparisons for indexed date columns over LIKE patterns when dealing with specific dates or ranges.
  • Monitor Performance: Utilize APM tools (like New Relic, as mentioned in the issue) to proactively identify slow queries and resource hogs.

Merchants experiencing slow report generation or high database load should consult with their development teams to investigate if this specific bug is contributing to their performance issues. A custom patch or an override of the problematic method could provide a significant performance boost until an official fix is released.

This deep dive into a core Magento 2 performance issue underscores the continuous need for vigilance and optimization in maintaining a high-performing e-commerce platform. Efficient database operations are not just a technical detail; they are fundamental to the speed, reliability, and profitability of your online store.

Start with the tools

Explore migration tools

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

Explore migration tools