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

Avoid running query in WC_Payments::should_load_stripe_billing_integration() on every page load #9717

Open
prettyboymp opened this issue Nov 14, 2024 · 0 comments
Labels
category: performance The issue/PR is related to performance. focus: architecture Product architecture and engineering best practices

Comments

@prettyboymp
Copy link
Contributor

The fallback query used in WC_Payments::should_load_stripe_billing_integration() currently runs on every page load. The decision from the query results should probably be cached, especially since it isn't a simple query and isn't extremely fast.

$result = wcs_get_orders_with_meta_query(
[
'status' => 'any',
'return' => 'ids',
'type' => 'shop_subscription',
'limit' => 1, // We only need to know if there are any - at least 1.
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
[
'key' => '_wcpay_subscription_id',
'compare' => 'EXISTS',
],
],
]
);

Resulting Query:

SELECT wp_wc_orders.id FROM wp_wc_orders INNER JOIN wp_wc_orders_meta AS meta0 ON ( wp_wc_orders.id = meta0.order_id ) WHERE 1=1 AND (wp_wc_orders.status IN ('wc-pending','wc-active','wc-on-hold','wc-cancelled','wc-switched','wc-expired','wc-pending-cancel')) AND (wp_wc_orders.type = 'shop_subscription') AND ((meta0.meta_key = '_wcpay_subscription_id')) GROUP BY wp_wc_orders.id ORDER BY wp_wc_orders.date_created_gmt DESC LIMIT 0, 1

Query explain:

+----+-------------+--------------+--------+---------------------------------------------+----------------+---------+-------------------------+------+---------------------------------------------------------------------+
| id | select_type | table        | type   | possible_keys                               | key            | key_len | ref                     | rows | Extra                                                               |
+----+-------------+--------------+--------+---------------------------------------------+----------------+---------+-------------------------+------+---------------------------------------------------------------------+
| 1  | SIMPLE      | meta0        | ref    | meta_key_value,order_id_meta_key_meta_value | meta_key_value | 258     | const                   | 1    | Using index condition; Using where; Using temporary; Using filesort |
| 1  | SIMPLE      | wp_wc_orders | eq_ref | PRIMARY,status,type_status,type_status_date | PRIMARY        | 8       | wpmu_d05.meta0.order_id | 1    | Using where                                                         |
+----+-------------+--------------+--------+---------------------------------------------+----------------+---------+-------------------------+------+---------------------------------------------------------------------+
@prettyboymp prettyboymp added the category: performance The issue/PR is related to performance. label Nov 14, 2024
@deepakpathania deepakpathania added the focus: architecture Product architecture and engineering best practices label Nov 14, 2024
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. focus: architecture Product architecture and engineering best practices
Projects
None yet
Development

No branches or pull requests

2 participants