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

Searching subscriptions is slow for merchants with a large number of subscriptions #183

Open
2 tasks done
Jinksi opened this issue Jul 1, 2022 · 3 comments
Open
2 tasks done
Labels
category: performance The issue/PR is related to performance. pr: needs review type: bug The issue is a confirmed bug.

Comments

@Jinksi
Copy link
Member

Jinksi commented Jul 1, 2022

Describe the bug

The Subscriptions search in wp-admin → WooCommerce → Subscriptions is very slow for merchants who have a large number of subscriptions. For example, searching through ~30,000 subscriptions can take as much as 30 seconds.

image

To Reproduce

  1. Generate a large number of subscriptions (method TBC).
  2. Navigate to the wp-admin page WooCommerce → Subscriptions.
  3. Execute a search using the search field in the top-right of the page, e.g. "john".
  4. Wait for the results and observe the duration of the query using Query Monitor. Look for the query called by WCS_Subscription_Data_Store_CPT::search_subscriptions().

Expected behavior

The search query that is called by WCS_Subscription_Data_Store_CPT::search_subscriptions() should take as little time as is reasonably possible.

Actual behavior

The search query that is called by WCS_Subscription_Data_Store_CPT::search_subscriptions() takes longer than is reasonably possible.

Product impact

Additional context

More info provided by @foliovision in PR #182:

We find that this query in WCS_Subscription_Data_Store_CPT::search_subscriptions() is way too slow:

SELECT DISTINCT p1.post_id
FROM wp_postmeta p1
WHERE p1.meta_value LIKE '%phrase%' AND p1.meta_key IN ('_order_key','_billing_address_index','_shipping_address_index','_billing_email')

Looking as the SQL EXPLAIN I can see that on our client website it has to check 4,000,000 fields. When I split that query into 4 individual queries - one for each meta_key, then it's much faster. The above takes 17 to 20 seconds.

@Jinksi Jinksi added type: bug The issue is a confirmed bug. pr: needs review category: performance The issue/PR is related to performance. labels Jul 1, 2022
@Jinksi Jinksi linked a pull request Jul 1, 2022 that will close this issue
1 task
@haszari
Copy link
Contributor

haszari commented Sep 4, 2022

@Jinksi can you open an issue for this on WCPay and WC Subscriptions so we can track and prioritise against products?

@kaushikasomaiya
Copy link

kaushikasomaiya commented Oct 5, 2023

One of our merchants reported this as well 7126887-zen

Merchant reported both queries here could be limited to shop_subscription post_type:

https://github.com/Automattic/woocommerce-subscriptions-core/blob/trunk/includes/data-stores/class-wcs-subscription-data-store-cpt.php#L535-L549

Example:

SELECT DISTINCT p1.post_id
FROM wp_postmeta p1
JOIN wp_posts posts ON posts.ID = p1.post_id
WHERE p1.meta_value LIKE '%standard%' 
AND p1.meta_key IN ('_billing_email')
AND posts.post_type = 'shop_subscription'

@OllieJones
Copy link

OllieJones commented Nov 24, 2023

The root performance problem is, of course, the unanchored meta_value LIKE '%searchterm%' SQL predicate. It can't exploit an index, and it searches the meta_value CLOB, so it is necessarily slow -- forcing the DBMS to scan many rows -- when there are lots of subscriptions. Switching to an anchored meta_value LIKE 'searchterm%' search without the leading % wildcard greatly improves performance at the cost of the ability to search for as many substrings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: performance The issue/PR is related to performance. pr: needs review type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants