dpps0715 2016-02-09 14:06
浏览 28
已采纳

需要了解查询帖子的复杂性

There's something I try to understand about the process of querying posts in wordpress:

Lets say I have this code:

$args = array{
    'cat' => 'animals',
    'posts_per_page' => 3
}

$the_query = new WP_Query($args);

So theoretically wordpress go to the database, going through all posts, and for each post, from the first to the last, it "asks" the post for it's category, if the answer is "animals" it grabs that post and move to the one after, if the category is something else, it skip that post and move to the one after. And repeat that process n times? Or, since I set posts_per_page to 3, after the third post that matchs "animals", the process it stops?

If the answer is the first, How can I optimize the code if, let's say I have 300 posts under "animals", but I need only the latest 3. How can I tell wordpress to stop checking after the third one that match "animals"?

I'm asking this since I'm working on a custom theme for site with more then 40000 posts, the homepage need to have at least 5 different queries like the one above, and I want to make it as efficient as possible.

I hope that's make sense.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongqiang1894 2016-02-09 14:20
    关注

    If you look into the WP core, you will see this piece of code in wp-includes/query.php, in the get_posts function:

    if ( empty($q['offset']) ) {
        $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', ';
    } else { // we're ignoring $page and using 'offset'
        $q['offset'] = absint($q['offset']);
        $pgstrt = $q['offset'] . ', ';
    }
    $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
    

    This mean that the posts_per_page parameter is translated to a basic sql LIMIT. So you don't have to worry for performance on this side.

    How does 'LIMIT' parameter work in sql?

    Considering the wp_terms_taxonomy.term_taxonomy_id, wp_terms_taxonomy.term_id_taxonomy, wp_term_relationships.term_taxonomy_id are all indexes the LIMIT should not select all rows, as far as I know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口