租号玩代理申请
A-A+

WordPress 10w+数据时, 解决SQL_CALC_FOUND_ROWS查询使网站变慢问题

2023年11月24日 建站相关 暂无评论 阅读 57 次

WordPress在查询post列表时,默认会同时把文章数量也查询出来,

使用这种方式的有:get_posts 、query_posts和WP_Query。

get_posts在4.6.1+已经不用SQL_CALC_FOUND_ROWS,但是query_posts和WP_Query还是会用,所以还须优化。

具体语句如下:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' ) ORDER BY wp_posts.post_date DESC LIMIT 0, 20
SELECT FOUND_ROWS()

这在网站数据量小的时候,不会引起什么问题,

但是当post数量到10w+的时候,这个就是一条必现的慢查询

首页、分类、标签、搜索页面,只要用到这几个函数,就都会使用SQL_CALC_FOUND_ROWS这个方式。

那么,如何解决?

禁用掉SQL_CALC_FOUND_ROWS用法,用一种更加高效的方式

这里我们用EXPLAIN方式,为什么用EXPLAIN而不是count(*)

具体代码如下,放在functions.php文件:

if (!function_exists('maizi_set_no_found_rows')) {
    /**
     * 设置WP_Query的 'no_found_rows' 属性为true,禁用SQL_CALC_FOUND_ROWS
     *
     * @param  WP_Query $wp_query WP_Query实例
     * @return void
     */
    function maizi_set_no_found_rows(\WP_Query $wp_query)
    {
        $wp_query->set('no_found_rows', true);
    }
}
add_filter('pre_get_posts', 'maizi_set_no_found_rows', 10, 1);


if (!function_exists('maizi_set_found_posts')) {
    /**
     * 使用 EXPLAIN 方式重构
     */
    function maizi_set_found_posts($clauses, \WP_Query $wp_query)
    {
        // Don't proceed if it's a singular page.
        if ($wp_query->is_singular()) {
            return $clauses;
        }

        global $wpdb;

        $where = isset($clauses['where']) ? $clauses['where'] : '';
        $join = isset($clauses['join']) ? $clauses['join'] : '';
        $distinct = isset($clauses['distinct']) ? $clauses['distinct'] : '';

        $wp_query->found_posts = (int)$wpdb->get_row("EXPLAIN SELECT $distinct * FROM {$wpdb->posts} $join WHERE 1=1 $where")->rows;

        $posts_per_page = (!empty($wp_query->query_vars['posts_per_page']) ? absint($wp_query->query_vars['posts_per_page']) : absint(get_option('posts_per_page')));

        $wp_query->max_num_pages = ceil($wp_query->found_posts / $posts_per_page);

        return $clauses;
    }
}
add_filter('posts_clauses', 'maizi_set_found_posts', 10, 2);

如果你要用COUNT语句,就用下面的代码:

<?PHP
/**
 * Plugin Name: Fix WordPress Slow Queries
 * Description: Fix WordPress Slow Queries
 * Author: Mahdi Akrami
 * Version: 1.0.0
 */

class FIX_WP_SLOW_QUERY {

        public static function init () {

                /**
                 * WP_Query
                 */

                add_filter ( 'found_posts_query', [ __CLASS__, 'add_found_rows_query' ], 999, 2 );

                add_filter ( 'posts_request_ids', [ __CLASS__, 'remove_found_rows_query' ], 999 );

                add_filter ( 'posts_pre_query', function ( $posts, \WP_Query $query ) {

                        $query->request = self::remove_found_rows_query ( $query->request );

                        return $posts;
                }, 999, 2 );

                add_filter ( 'posts_clauses', function ( $clauses, \WP_Query $wp_query ) {

                        $wp_query->fw_clauses = $clauses;

                        return $clauses;
                }, 999, 2 );

        }

        public static function remove_found_rows_query ( $sql ) {

                return str_replace ( ' SQL_CALC_FOUND_ROWS ', '', $sql );
        }

        public static function add_found_rows_query ( $sql, WP_Query $query ) {

                global $wpdb;

                $distinct = $query->fw_clauses['distinct'] ?? '';
                $join     = $query->fw_clauses['join'] ?? '';
                $where    = $query->fw_clauses['where'] ?? '';
                $groupby  = $query->fw_clauses['groupby'] ?? '';

                $count = 'COUNT (*)';

                if ( ! empty ( $groupby ) ) {
                        $count = "COUNT ( distinct $groupby )";
                }

                return "
                        SELECT $distinct $count
                        FROM {$wpdb->posts} $join
                        WHERE 1=1 $where
                ";
        }

}

FIX_WP_SLOW_QUERY::init ();

这样就可以了,记得清除缓存。

京东空调大促销