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

Issue/4795 prevent related order duplicate queries on subscriptions list table #788

Conversation

james-allan
Copy link
Contributor

Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4795

Description

This PR reduces the number of duplicate queries running on the WooCommerce → Subscriptions list table, decreasing them from 137 to 60. This PR focuses on two key functions:

1. Optimizing get_date_column_content()

  • Previously, this function called get_time() multiple times. When rendering the Last Order Date column, each call triggered a get_related_orders() query, resulting in redundant database reads.
  • The changes in this PR minimize unnecessary get_time() calls when generating date columns.

2. Improving WC_Subscription::get_related_orders()

  • When get_related_orders() was called with multiple order types, it triggered separate get_related_order_ids() queries for each type.
  • That would then subsequently result in a call to WCS_Related_Order_Store::instance()->get_related_order_ids() for each type and therefore a full read from the subscription meta table.
    Example:
    $subscription->get_related_orders( ..., [ 'renewal', 'switch' ] );
  • Previously, this resulted in two database reads. Now, the related order caches have been updated to allow that to be 1 database read.

Summary of Changes

  • Reduced unnecessary get_time() calls in get_date_column_content().
  • Updated WC_Subscription::get_related_order_ids() and WCS_Related_Order_Store to allow fetching multiple related orders caches with a single database read.

How to test this PR

  1. Install Query Monitor.
  2. Go to WooCommerce → Subscriptions
  3. Open the Query Monitor window and go to Database Queries → Duplicate Queries
  4. Note the number of duplicate queries.
    • Each subscription in the list table is causing 6 calls to get_related_order_metadata()

Screenshot 2025-02-19 at 3 29 56 pm
137 duplicate queries.

  1. Checkout this branch and repeat the steps above.
  2. The number of duplicate queries should drop by roughly 4 x the number of subscriptions you have in the list table (20).
  3. There should be no difference in content or functionality on the list table.

Product impact

  • Added changelog entry (or does not apply)
  • Will this PR affect WooCommerce Subscriptions? yes/no/tbc, add issue ref
  • Will this PR affect WooCommerce Payments? yes/no/tbc, add issue ref
  • Added deprecated functions, hooks or classes to the spreadsheet

@james-allan
Copy link
Contributor Author

I've split this PR into two parts. The date-related changes are straightforward and bring the most significant improvement, while the related order changes have a broader impact and offer less immediate benefit.

Given the difference in scope and risk between these changes, keeping them in a single PR didn’t make sense.

The two component PRs are:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant