Skip to content

Instantly share code, notes, and snippets.

@lewg
Created October 24, 2013 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewg/7138908 to your computer and use it in GitHub Desktop.
Save lewg/7138908 to your computer and use it in GitHub Desktop.
WordPress fix for slow non-subscriber user query.
<?php
// Fix non-subscriber query for large data
function kw_adjust_for_non_subscribers($args) {
global $wpdb;
if ($args['join'] == ' INNER JOIN '.$wpdb->usermeta.' ON (katw_wp_users.ID = '.$wpdb->usermeta.'.user_id)' AND
$args['where'] == ' AND ( ('.$wpdb->usermeta.'.meta_key = \'katw_wp_user_level\' AND CAST('.$wpdb->usermeta.'.meta_value AS CHAR) != \'0\') )') {
// Switch to RIGHT JOIN
$args['join'] = ' RIGHT JOIN '.$wpdb->usermeta.' ON (katw_wp_users.ID = '.$wpdb->usermeta.'.user_id)';
}
return($args);
}
add_filter( 'get_meta_sql', 'kw_adjust_for_non_subscribers');
@lewg
Copy link
Author

lewg commented Oct 24, 2013

MySQL doesn't like the INNER JOIN that WordPress builds while looking for that info. Switching it to a RIGHT JOIN gets the same results, and a much better execution plan. (for 2 million users, it was 4s instead of 91s). This is definitely a hacky fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment