Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 33 forks source link

When HPOS is disabled use the users subscription cache when fetching subscriptions by customer ID #468

Closed mattallan closed 1 year ago

mattallan commented 1 year ago

Fixes #466

Description

In https://github.com/Automattic/woocommerce-subscriptions-core/pull/171 we removed a section of our wcs_get_subscriptions() function which used get subscriptions by customer ID using the user's subscription cache and then setting this list of IDs in post__in to improve performance of this query.

While working on HPOS changes we removed the cache/post__in approach as it was no longer needed due to improvements with the new order tables, however, this now means non-HPOS stores are now inefficiently getting subscriptions by customer ID.

The code we removed:

// Maybe filter to a specific user
if ( 0 != $args['customer_id'] && is_numeric( $args['customer_id'] ) ) {
    $users_subscription_ids = WCS_Customer_Store::instance()->get_users_subscription_ids( $args['customer_id'] );
    $query_args             = WCS_Admin_Post_Types::set_post__in_query_var( $query_args, $users_subscription_ids );
};

For large stores without HPOS, this can be a pretty significant hit to performance when using wcs_get_subscriptions() with the customer_id arg, so this PR brings back the user cache + post__in approach we had previously when HPOS is disabled.

Product impact

mattallan commented 1 year ago

I've added a changelog entry. Thanks @james-allan for the review.