41827-improve-coupon-usage-by-user-query-performance (#43731)

The sql query can be slow on big data sets, because the post meta value is type of string, and we are quering with a number.
This commit is contained in:
Vlad Bozdog 2024-01-17 12:29:28 +02:00 committed by GitHub
parent 90b3e11dba
commit 4966697b7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Fix wpdb->prepare argument type for coupons query

View File

@ -441,7 +441,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
global $wpdb;
$usage_count = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT( meta_id ) FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_used_by' AND meta_value = %d;",
"SELECT COUNT( meta_id ) FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_used_by' AND meta_value = %s;",
$coupon->get_id(),
$user_id
)