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

2.6k MySQL , 3评论

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

使用这种方式的有:get_posts 、query_postsWP_Query

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

具体语句如下:

SELECT SQL_CALC_FOUND_ROWS tmpe04d7e_posts.ID FROM tmpe04d7e_posts WHERE 1=1 AND tmpe04d7e_posts.post_type = 'post' AND (tmpe04d7e_posts.post_status = 'publish' ) ORDER BY tmpe04d7e_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 );

 

参考资料:

  1. WordPress: SQL_CALC_FOUND_ROWS, why it’s slow and what to do about it
  2. Speed up WordPress WP_Query and query_posts functions

3条评论

绿
绿软吧(lvr8) says: 回复

感谢分享,谢谢站长!!

D
Daniel says: 回复

maizi_set_found_posts 是什么? 似乎没有解决问题,站长可否详细再补充说说?

风君子 says: 回复

wp5.7.2 没效果啊,慢查询日志里都是类似语句,而且cpu经常100%

回复 绿软吧(lvr8) 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

昵称 *