Created
October 24, 2013 15:08
-
-
Save lewg/7138908 to your computer and use it in GitHub Desktop.
WordPress fix for slow non-subscriber user query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.