Magento 2.4+ Async Grid Performance: The Full Table Scan Dilemma

Unraveling Magento 2.4+ Async Grid Performance: The Full Table Scan Dilemma

At Shopping Mover, we constantly monitor the Magento ecosystem for insights that can impact store performance and migration strategies. A recent GitHub issue (#41836) has brought to light a significant performance bottleneck in Magento 2.4-develop and potentially later versions, specifically concerning the asynchronous grid reconciliation process for sales entities (orders, invoices, shipments, credit memos).

The Core Problem: Unbounded Query Leading to Full Table Scans

The issue, reported by lbajsarowicz, details how the asynchronous grid reconciliation query, executed by Magento\Sales\Model\ResourceModel\Provider\UpdatedAtListProvider::getIdsWithCutoff(), scans the entire sales_order table every minute. This occurs when dev/grid/async_indexing is enabled, particularly impacting large Magento instances with hundreds of thousands or millions of orders.

The problematic query, which lacks a lower bound on the updated_at timestamp, looks like this:

SELECT main_table.entity_id
FROM sales_order AS main_table
INNER JOIN sales_order_grid AS grid_table
  ON main_table.entity_id = grid_table.entity_id
  AND (main_table.updated_at <> grid_table.updated_at OR grid_table.updated_at IS NULL)
WHERE (main_table.updated_at <= '');

As demonstrated by the author's benchmarks, this unbounded query leads to a full scan of both sales_order_grid and sales_order tables. For a table with 1,000,000 rows, this translates to a query time of approximately 650 ms. In contrast, adding a simple 5-minute lower bound reduces the query time to a mere 0.26 ms, examining only 50 rows instead of 2,000,000.

Here's a quick comparison of the performance impact:

| Rows      | Variant                     | Rows examined              | Median of 5 runs |
|-----------|-----------------------------|----------------------------|------------------|
| 1,000,000 | current query               | 1,000,000 grid + 1,000,000 main | 650 ms           |
| 1,000,000 | with a 5 minute lower bound | 50 + 50                    | 0.26 ms          |
| 250,000   | current query               | 250,000 + 250,000          | 132 ms           |
| 250,000   | with a 5 minute lower bound | 50 + 50                    | 0.30 ms          |

The Underlying Cause: A Trade-off Between Correctness and Performance

The root cause of this performance regression lies in a previous fix (ACP2E-5516, related to #40803). This fix removed the updated_at >= condition from the provider. While this change was crucial for correctness, preventing rows with delayed commits from being permanently skipped during reconciliation, it inadvertently reintroduced the full table scan issue that the watermark was originally designed to prevent (#40282, #40271).

This creates a difficult trade-off: ensuring all rows are eventually synchronized (correctness) versus maintaining optimal database performance. Without a safety net, rows whose grid copies drift too far behind could remain out of sync indefinitely.

Proposed Solutions for Maintainers

The issue author, lbajsarowicz, has thoughtfully presented three potential solutions, offering to submit a Pull Request for the chosen approach:

  1. Hybrid Bounded/Unbounded Scan: Implement a bounded window (e.g., last N minutes) for most runs, supplemented by a less frequent (hourly or daily) full unbounded pass to catch older discrepancies.
  2. Rotating entity_id Cursor: Combine a bounded window with a rotating entity_id cursor. This would scan the full table in slices over time, similar to how UpdatedIdListProvider already handles missing rows, ensuring bounded reads per run while eventually covering the entire table.
  3. Changelog Table/MVIEW: Utilize a changelog table or a materialized view (MVIEW) subscription on sales_order.updated_at to only reconcile changed entity IDs, offering a more event-driven approach.

It's also noted that some related 'dead code' exists: Grid::refreshBySchedule() still saves the last processed updated_at, but this value is no longer used. This could potentially be removed or repurposed as a watermark for option 1.

Community Impact and Next Steps

This issue highlights a critical performance challenge for large-scale Magento 2.4+ deployments. While the GitHub comments are primarily automated bot responses, the detailed analysis and proposed solutions by the issue author provide a clear path forward. For merchants and developers running or migrating large Magento stores, understanding this bottleneck is crucial for maintaining a healthy and performant system. We encourage the Magento community to engage with this issue and support its resolution.

Start with the tools

Explore migration tools

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

Explore migration tools