我们有一个wordpress,有100万个产品帖子和15000个分页页面,我们优化了所有网站,性能良好,但我们检测到wordpress上的存档分页速度很慢,我在存档页面上的查询示例如下:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = \'prod_bb\'
AND (wp_posts.post_status = \'publish\'
OR wp_posts.post_status = \'private\')
ORDER BY ID
LIMIT 1043000, 70
这个查询并没有优化到可以推断70个帖子扫描了整个数据库,经过一些研究,我们发现了一篇关于
wordpress.com, 如何管理分页
但尝试进行一些测试时,我无法运行代码,这是我实现的测试代码:
<?php
// Start with 0
$last_id = 0;
do {
$blogs = $wpdb->get_results( $wpdb->prepare(
"SELECT wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = \'prod_bb\'
AND (wp_posts.post_status = \'publish\'
OR wp_posts.post_status = \'private\')
AND wp_posts.ID > %d LIMIT 70;",
$last_id // Use the last ID to start after
) );
foreach ( $blogs as $blog ) {
// Do your thing!
// get_template_part( \'template-parts/content-index\', get_post_format() );
// ...
// Record the last ID for the next loop
$last_id = $blog->wp_posts.ID;
}
// Do it until we have no more records
} while ( ! empty( $blogs ) );
?>
有了这段代码,我的服务器的cpu达到了100%,它仍处于加载状态,没有显示任何内容