diff --git a/.changelogs/orders-transactions-months-timeout.yml b/.changelogs/orders-transactions-months-timeout.yml new file mode 100644 index 0000000000..8e9d2346fa --- /dev/null +++ b/.changelogs/orders-transactions-months-timeout.yml @@ -0,0 +1,3 @@ +significance: patch +type: fixed +entry: "Fixed the Orders & Transactions screen timing out on large stores while the 11.0.0 database update is still pending." diff --git a/includes/admin/reporting/tables/llms.table.orders.transactions.php b/includes/admin/reporting/tables/llms.table.orders.transactions.php index fdd125faf7..eb2a73830b 100644 --- a/includes/admin/reporting/tables/llms.table.orders.transactions.php +++ b/includes/admin/reporting/tables/llms.table.orders.transactions.php @@ -1276,12 +1276,20 @@ private function get_available_months() { ORDER BY year DESC, month DESC" ); } else { - $months = $wpdb->get_results( - "SELECT DISTINCT YEAR( p.post_date ) AS year, MONTH( p.post_date ) AS month - FROM {$wpdb->posts} AS p - WHERE p.post_status NOT IN ( 'auto-draft', 'trash' ) - AND ( p.post_type = 'llms_transaction' OR ( p.post_type = 'llms_order' AND p.ID NOT IN ( SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_llms_order_id' ) ) ) - ORDER BY year DESC, month DESC" + // An `ID NOT IN ( SELECT meta_value ... )` subquery runs once per order and takes minutes on + // large stores, so exclude the already-resolved list of order IDs instead. + // With no such orders, exclude ID 0, which matches no post, so the list always has a placeholder. + $orders_with_txns = $this->get_order_ids_with_transactions(); + $orders_with_txns = $orders_with_txns ? $orders_with_txns : array( 0 ); + $months = $wpdb->get_results( + $wpdb->prepare( + "SELECT DISTINCT YEAR( p.post_date ) AS year, MONTH( p.post_date ) AS month + FROM {$wpdb->posts} AS p + WHERE p.post_status NOT IN ( 'auto-draft', 'trash' ) + AND ( p.post_type = 'llms_transaction' OR ( p.post_type = 'llms_order' AND p.ID NOT IN ( " . implode( ',', array_fill( 0, count( $orders_with_txns ), '%d' ) ) . ' ) ) ) + ORDER BY year DESC, month DESC', + $orders_with_txns + ) ); } wp_cache_set( $cache_key, $months, self::CACHE_GROUP, HOUR_IN_SECONDS );