0

I've got a simple custom WP_Query on a custom post type page that shows a collection of posts:

$t_query = new WP_Query($filters['filtered_args_for_query']);

$wpdb->last_query outputs:

SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (203046,205067,205540,206297,207374,207391,207996,209090,210979,211141,211153,211166) ORDER BY meta_id ASC

Whenever I place a simple Loop into my template, it takes 9 seconds to process the page (otherwise, 1 second):

if($t_query->have_posts()){
   while($t_query->have_posts()){
      $t_query->the_post();
   }
}

There are some 60 000 posts and 250 000 meta entries in my database. BUT, on other pages (category pages), that have the same custom query, my server serves pages in just a second or so. There, $wpdb->last_query returns:

SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (187887,189775,189853,190680,192412,196893,215373) ORDER BY meta_id ASC

To me, they both seem pretty much alike.

Can someone tell me what makes such a difference, or where to search? There must be something in the Loop that makes the difference, since the pages for the custom post type and the category load just the same when I remove the Loop, and since (for debug purposes) my Loop doesn't do anything except preparing posts.

Edit. An example of $filters['filtered_args_for_query'] is:

  ["post_type"]=>
  string(16) "tdlrm_store_item"
  ["post__in"]=>
  array(42) {
    [0]=>
    string(6) "203046"
    [1]=>
    string(6) "203514"
    [2]=>
    string(6) "205034"
    [3]=>
    string(6) "205067"
    [4]=>
    string(6) "205286"
    [5]=>
    string(6) "205321"
    [6]=>
    string(6) "205540"
    [7]=>
    string(6) "205596"
    [8]=>
    string(6) "205616"
    [9]=>
    string(6) "205637"
    [10]=>
    string(6) "206297"
    [11]=>
    string(6) "206325"
    [12]=>
    string(6) "206887"
    [13]=>
    string(6) "207229"
    [14]=>
    string(6) "207232"
    [15]=>
    string(6) "207253"
    [16]=>
    string(6) "207322"
    [17]=>
    string(6) "207374"
    [18]=>
    string(6) "207391"
    [19]=>
    string(6) "207828"
    [20]=>
    string(6) "207996"
    [21]=>
    string(6) "208015"
    [22]=>
    string(6) "208027"
    [23]=>
    string(6) "208178"
    [24]=>
    string(6) "208186"
    [25]=>
    string(6) "208300"
    [26]=>
    string(6) "208306"
    [27]=>
    string(6) "208410"
    [28]=>
    string(6) "209244"
    [29]=>
    string(6) "210979"
    [30]=>
    string(6) "210991"
    [31]=>
    string(6) "211093"
    [32]=>
    string(6) "211141"
    [33]=>
    string(6) "211150"
    [34]=>
    string(6) "211153"
    [35]=>
    string(6) "211157"
    [36]=>
    string(6) "211165"
    [37]=>
    string(6) "211166"
    [38]=>
    string(6) "211521"
    [39]=>
    string(6) "217942"
    [40]=>
    string(6) "225640"
    [41]=>
    string(6) "209090"
  }
  ["posts_per_page"]=>
  int(12)
  ["paged"]=>
  int(1)
  ["tdlrm_commands"]=>
  array(2) {
    ["do"]=>
    string(16) "orderby_filter_1"
    ["key"]=>
    string(9) "tdlrm_mp_"
  }
  ["meta_query"]=>
  array(3) {
    ["relation"]=>
    string(2) "OR"
    ["mp_exists"]=>
    array(2) {
      ["key"]=>
      string(9) "tdlrm_mp_"
      ["type"]=>


      string(7) "NUMERIC"
        }
        ["mp_not_exists"]=>
        array(2) {
          ["key"]=>
          string(9) "tdlrm_mp_"
          ["compare"]=>
          string(10) "NOT EXISTS"
        }
      }
      ["orderby"]=>
      bool(false)
    }
7
  • What is $filters['filtered_args_for_query']? Nov 16, 2022 at 5:03
  • Hey Jacob. It's just some WP_Query arguments that depend on my current needs, including some custom commands to filter the MySQL query. I've added an example to my question. On a taxonomy page they would not have the post__in argument, but include a tax_query instead.
    – Artem
    Nov 16, 2022 at 8:35
  • Well the arguments are what's important, because that's what would be making the query slow. Your example seems like a fairly complex query. What is tdlrm_commands? Nov 16, 2022 at 9:12
  • Jacob, probably this is best explained in my other question: wordpress.stackexchange.com/questions/396024/…. You see, the resulting raw MySQL query is very simple and takes only 0.0011 in PHPMyAdmin. It is the iteration over the results that is slow.
    – Artem
    Nov 16, 2022 at 12:46
  • Wait. What if I use $wpdb->last query in the loop...
    – Artem
    Nov 16, 2022 at 12:47

1 Answer 1

1

I found a solution to my question. I used the following code:

global $wpdb;
if($t_query->have_posts()){
   while($t_query->have_posts()){
      $t_query->the_post();
      echo $wpdb->last_query."\r\n";
   }
}

and it showed there was an action somewhere hooked to the_post hook, sending requests to update some meta every time a post was processed.

So, the answer to my question is: I should have used $wpdb->last_query inside the loop to find out what was going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.