Automattic/woocommerce-payments

[Performance] Slow admin queries caused by `WC_Payments_Incentives_Service::get_store_context()`

Opened this issue · 0 comments

Problem:

The queries caused by WC_Payments_Incentives_Service::get_store_context() are slow and uncached contributing to slowness while using the admin.

Details:

When a site has the WooCommerce Payments plugin active, but not yet setup, there are a couple of queries that are poor performing on larger stores that are being run on every page load caused by the calls to WC_Payments_Incentives_Service::get_store_context(). The following are example debugging information for the queries run on a site with 350K orders.

Query: 680.3 ms
SELECT wp_wc_orders.id FROM wp_wc_orders WHERE 1=1 AND (wp_wc_orders.status IN ('wc-completed','wc-processing')) AND (wp_wc_orders.type IN ('shop_order','shop_order_refund')) AND ( (
wp_wc_orders.date_created_gmt >= '2024-10-01 14:21:50'
)) GROUP BY wp_wc_orders.id ORDER BY wp_wc_orders.date_created_gmt DESC LIMIT 0, 1

Trace: 
require('wp-admin/menu.php'), require_once('wp-admin/includes/menu.php'), do_action('admin_menu'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Payments_Incentives_Service->add_payments_menu_badge, WC_Payments_Incentives_Service->get_cached_connect_incentive, WC_Payments_Incentives_Service->get_store_context, wc_get_orders, WC_Order_Query->get_orders, WC_Data_Store->__call, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore->query, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery->__construct, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery->run_query, QM_DB->query

---

Query: 789.5 ms
SELECT wp_wc_orders.id FROM wp_wc_orders WHERE 1=1 AND (wp_wc_orders.status IN ('wc-pending','wc-processing','wc-on-hold','wc-completed','wc-cancelled','wc-refunded','wc-failed','wc-checkout-draft')) AND (wp_wc_orders.type IN ('shop_order','shop_order_refund')) AND (wp_wc_orders.payment_method = 'woocommerce_payments') GROUP BY wp_wc_orders.id ORDER BY wp_wc_orders.date_created_gmt DESC LIMIT 0, 1

Trace:

require('wp-admin/menu.php'), require_once('wp-admin/includes/menu.php'), do_action('admin_menu'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Payments_Incentives_Service->add_payments_menu_badge, WC_Payments_Incentives_Service->get_cached_connect_incentive, WC_Payments_Incentives_Service->get_store_context, WC_Payments_Incentives_Service->has_wcpay, wc_get_orders, WC_Order_Query->get_orders, WC_Data_Store->__call, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore->query, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery->__construct, Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery->run_query, QM_DB->query