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

Fixed slow allowed users query due to use of regex. #21

Merged
merged 1 commit into from
Nov 9, 2021

Conversation

bogdanarizancu
Copy link
Contributor

Ticket

Description

  • the problem was using regexp, it looks like this was slowing the query by ~ 0.5-1 sec

Before

image

After

image

$allowedUserIds = implode(',', $wpdb->get_col(
$wpdb->prepare("SELECT u.ID FROM {$wpdb->prefix}users u WHERE u.user_email REGEXP ('%s')", implode('|', $allowedEmails)
)));
$wpdb->prepare("SELECT u.ID FROM {$wpdb->prefix}users u WHERE u.user_email IN ({$emailsPlaceholder})", $allowedEmails)
Copy link

@fabianmarz fabianmarz Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but why did you use {$wpdb->prefix}users instead of {$wpdb->users}? 🤔

Suggested change
$wpdb->prepare("SELECT u.ID FROM {$wpdb->prefix}users u WHERE u.user_email IN ({$emailsPlaceholder})", $allowedEmails)
$wpdb->prepare("SELECT u.ID FROM {$wpdb->users} u WHERE u.user_email IN ({$emailsPlaceholder})", $allowedEmails)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabianmarz only for the sake of consistency, since tables like woocommerce_order_items are not available like that.

@bogdanarizancu bogdanarizancu merged commit e786cf7 into main Nov 9, 2021
@bogdanarizancu bogdanarizancu deleted the fix/slow-query-regex-bogdan branch November 9, 2021 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants