Magento 2

Magento 2 Performance Deep Dive: Unmasking the Hidden Cost of Inefficient Report Aggregation

Unmasking a Silent Killer: The Magento 2 Report Aggregation Performance Drain

As e-commerce migration experts at Shopping Mover, our mission extends beyond simply moving stores; we're dedicated to ensuring they thrive with optimal performance. We constantly scrutinize the Magento ecosystem for insights that can empower merchants and developers. A recent GitHub issue (magento/magento2/#41837), reported by lbajsarowicz and identified through rigorous production performance monitoring, has brought to light a significant, yet often hidden, bottleneck within Magento 2's report statistics refresh process. This issue, particularly impactful for stores with extensive datasets, illustrates how a seemingly minor SQL query construction can lead to substantial performance degradation, affecting everything from server load to the accuracy of business intelligence.

The Root Cause: Inefficient SQL LIKE Chains in Report Generation

The core of this performance drain lies in how Magento 2 aggregates crucial report data, specifically for tables like sales_bestsellers_aggregated_daily. When the cron job aggregate_sales_report_bestsellers_data runs to refresh these vital statistics, it invokes the Magento\Sales\Model\ResourceModel\Report\Bestsellers::aggregate() method. For Magento stores with years of sales history – easily accumulating millions of rows – this process becomes incredibly resource-intensive and slow.

The problem originates within the _makeConditionFromDateRangeSelect() method of Magento\Reports\Model\ResourceModel\Report\AbstractReport. Instead of constructing an efficient condition for filtering date ranges, it builds a series of LIKE 'YYYY-MM-DD' clauses, joined by OR, for each individual date within the refresh period. For instance, refreshing a two-day range might generate a condition like: period LIKE '2026-11-09' OR period LIKE '2026-11-10'.

Technical Deep Dive: LIKE vs. IN – A Critical Performance Divide

The issue's author meticulously details the profound performance impact of this approach. When a database optimizer (like those in MariaDB or MySQL) encounters a chain of LIKE conditions on a DATE column, even if that column is indexed, it often fails to utilize the index effectively. This typically results in a full table scan, indicated by an EXPLAIN output showing type=ALL.

Consider the reported scenario: a sales_bestsellers_aggregated_daily table with approximately 1.2 million rows. Refreshing a two-day range using the LIKE chain resulted in:

  • type=ALL: Full table scan.
  • Rows examined: Approximately 1.2 million.
  • Execution time: 0.27 seconds per statement.

Contrast this with the same statement using an IN clause, which is the expected and efficient method:

SELECT ... FROM sales_bestsellers_aggregated_daily WHERE store_id <> 0 AND period IN ('2026-11-09', '2026-11-10') GROUP BY period, product_id;

With the IN clause, the database optimizer can leverage the unique key on (period, store_id, product_id), resulting in:

  • type=range: Efficient index usage.
  • Rows examined: Only 2402 (a fraction of the total).
  • Execution time: A mere 0.002 seconds.

This staggering difference – a 135x speed improvement – highlights how critical proper SQL query construction is for database performance, especially with large datasets common in thriving e-commerce stores.

Beyond Bestsellers: Widespread Impact Across Magento 2 Reports

The problem isn't isolated to just bestsellers. The issue notes that this inefficient condition is also used by the second-phase WHERE clauses of other critical Magento 2 reports, including:

  • Invoiced
  • Refunded
  • Shipping
  • Tax (based on Createdat)
  • SalesRule (based on Rule/Createdat)

Furthermore, similar performance issues were observed with HAVING clauses on DATE(CONVERT_TZ(...)) aliases in source queries. While indexes don't typically apply to HAVING clauses on derived columns, the evaluation is still significantly cheaper with an IN clause (4.5 seconds) compared to a LIKE chain (18 seconds) for 1500 dates.

The Real-World Consequences for Magento Merchants

For merchants operating on Magento Open Source or Adobe Commerce, these technical inefficiencies translate directly into tangible business problems:

  • Slow Cron Jobs: Report aggregation cron jobs can run for hours, consuming valuable server resources and potentially overlapping with other critical tasks.
  • Resource Exhaustion: Prolonged full table scans put immense pressure on database servers, leading to high CPU usage, increased I/O, and potential slowdowns across the entire store.
  • Stale or Incomplete Reports: If cron jobs time out or fail due to performance issues, your business intelligence reports might not be up-to-date, leading to misinformed decisions.
  • Higher Hosting Costs: Inefficient queries demand more powerful (and expensive) server infrastructure to cope with the load.

Identifying such issues often requires sophisticated production performance monitoring tools like New Relic, as demonstrated by Fast White Cat, who initially identified this problem.

Identifying and Addressing the Bottleneck: What You Can Do

While this issue is a core Magento concern that ideally requires an official patch, there are steps developers and merchants can take:

  • For Developers:
    • Monitor SQL Queries: Regularly use EXPLAIN on slow queries identified through profiling tools (e.g., Blackfire, New Relic) to understand how the database executes them. Look for type=ALL on large tables.
    • Contribute to Magento Core: Engage with the Magento community on GitHub. Contributing a fix or advocating for its prioritization is the most sustainable solution. The issue notes that the DELETE path for the same refresh already uses IN (ACP2E-4363), indicating a precedent for this improvement.
    • Consider Temporary Overrides (with caution): For critical performance needs, a custom module could potentially override the _makeConditionFromDateRangeSelect() method or the specific aggregate methods to implement IN clauses. However, this involves modifying core behavior and requires careful testing and maintenance during future Magento updates.
  • For Merchants:
    • Prioritize Performance Audits: Regularly audit your Magento store's performance, especially cron job execution times and database load.
    • Stay Updated: Keep your Magento installation updated to the latest stable version. Official patches for such issues are often included in minor or patch releases.
    • Partner with Experts: Work with experienced Magento development and migration partners like Shopping Mover. We have the expertise to identify, diagnose, and implement solutions for complex performance bottlenecks, ensuring your store runs efficiently.

Shopping Mover's Expertise in Action: Ensuring Peak Magento Performance

At Shopping Mover, our deep understanding of Magento's architecture, from its PHP codebase to its database interactions, allows us to proactively identify and resolve such intricate performance issues. Whether you're migrating to Magento 2, upgrading an existing installation, or simply seeking to optimize your current store, our team leverages tools and expertise to ensure your e-commerce platform operates at peak efficiency.

We believe that a fast, reliable store is the foundation of online success. By addressing underlying technical debt like inefficient SQL queries, we help merchants avoid costly downtime, improve customer experience, and ultimately drive better business outcomes. Don't let hidden performance drains slow down your growth.

If you suspect your Magento store is suffering from similar performance bottlenecks or are planning a migration, reach out to Shopping Mover. Our experts are ready to help you unlock your store's full potential.

Share:

Start with the tools

Explore migration tools

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

Explore migration tools