WordPress 在查询 post 列表时,默认会同时把文章数量也查询出来,使用这种方式的有:get_posts 、query_posts 和 WP_Query。
文章转自:歪麦博客
SQL_CALC_FOUND_ROWS 查询简介
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 这个方式。
如何解决 10w+ 数据 WordPress SQL_CALC_FOUND_ROWS 查询使网站变慢问题?
禁用 SQL_CALC_FOUND_ROWS,用一种更加高效的方式。这里我们用 EXPLAIN 方式,为什么用 EXPLAIN 而不是 count(*) ?
具体代码如下,放在主题 functions.php 文件即可:
if ( ! function_exists( 'theme_set_no_found_rows' ) ) {
/**
* 设置WP_Query的 'no_found_rows' 属性为true,禁用 SQL_CALC_FOUND_ROWS
*
* @param WP_Query $wp_query WP_Query 实例
* @return void
*/
function theme_set_no_found_rows(\WP_Query $wp_query)
{
$wp_query->set('no_found_rows', true);
}
}
add_filter( 'pre_get_posts', 'theme_set_no_found_rows', 10, 1 );
if ( ! function_exists( 'theme_set_found_posts' ) ) {
//使用 EXPLAIN 方式重构
function theme_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', 'theme_set_found_posts', 10, 2 );
参考资料
WordPress: SQL_CALC_FOUND_ROWS, why it’s slow and what to do about it
Speed up WordPress WP_Query and query_posts functions
参考资料备份下载:绿皮火车
Comments:0