get_post() not fetching from post cache after update_post_caches()

View: New views
10 Messages — Rating Filter:   Alert me  

get_post() not fetching from post cache after update_post_caches()

by William Canino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

WordPress wizards, would you have a look at this code:

$my_query = new WP_Query(array('post__in' => array(1,2,3)));
update_post_caches($my_query->posts);
echo $get_permalink(1);
echo $get_the_title(2);

Do you know why WordPress is still making SQL queries on lines 3 and
4? Shouldn't they be already in the object cache?
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by scribu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It looks like a bug.

But why would you manually call update_post_caches?


On Thu, Oct 1, 2009 at 8:39 AM, William Canino <
william.canino@...> wrote:

> WordPress wizards, would you have a look at this code:
>
> $my_query = new WP_Query(array('post__in' => array(1,2,3)));
> update_post_caches($my_query->posts);
> echo $get_permalink(1);
> echo $get_the_title(2);
>
> Do you know why WordPress is still making SQL queries on lines 3 and
> 4? Shouldn't they be already in the object cache?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



--
http://scribu.net
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by William Canino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm coding a Yet-Another-Clickable-List-of-Posts-With-Something-in-Common.
 The amount of SQL queries my current code is making is driving me
nuts.

2009/10/1 scribu <scribu@...>:

> It looks like a bug.
>
> But why would you manually call update_post_caches?
>
>
> On Thu, Oct 1, 2009 at 8:39 AM, William Canino <
> william.canino@...> wrote:
>
>> WordPress wizards, would you have a look at this code:
>>
>> $my_query = new WP_Query(array('post__in' => array(1,2,3)));
>> update_post_caches($my_query->posts);
>> echo $get_permalink(1);
>> echo $get_the_title(2);
>>
>> Do you know why WordPress is still making SQL queries on lines 3 and
>> 4? Shouldn't they be already in the object cache?
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers@...
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>
>
>
> --
> http://scribu.net
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by scribu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

And in generating this list you require altering posts? You should be
using filters instead.

On 10/1/09, William Canino <william.canino@...> wrote:

> I'm coding a Yet-Another-Clickable-List-of-Posts-With-Something-in-Common.
>  The amount of SQL queries my current code is making is driving me
> nuts.
>
> 2009/10/1 scribu <scribu@...>:
>> It looks like a bug.
>>
>> But why would you manually call update_post_caches?
>>
>>
>> On Thu, Oct 1, 2009 at 8:39 AM, William Canino <
>> william.canino@...> wrote:
>>
>>> WordPress wizards, would you have a look at this code:
>>>
>>> $my_query = new WP_Query(array('post__in' => array(1,2,3)));
>>> update_post_caches($my_query->posts);
>>> echo $get_permalink(1);
>>> echo $get_the_title(2);
>>>
>>> Do you know why WordPress is still making SQL queries on lines 3 and
>>> 4? Shouldn't they be already in the object cache?
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers@...
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>>
>>
>>
>> --
>> http://scribu.net
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers@...
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


--
http://scribu.net
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by Otto-19 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You shouldn't need to call update_post_caches, as the
WP_Query->query() function does that automatically anyway. query.php,
line 2359 (or thereabouts).

As for the rest, I don't know. The get_post function should indeed
check the cache first.

Call global $wp_object_cache; $wp_object_cache->stats(); to see the
contents of the cache before and after each call. Might help you with
debugging.

-Otto
Sent from Memphis, TN, United States


On Thu, Oct 1, 2009 at 11:47 AM, William Canino
<william.canino@...> wrote:
> I'm coding a Yet-Another-Clickable-List-of-Posts-With-Something-in-Common.
>  The amount of SQL queries my current code is making is driving me
> nuts.
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by William Canino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for having a look.  I have isolated the issue. This code

function my_test_function() {
        global $wpdb, $wp_object_cache;
        $comments = $wpdb->get_results("Some query on wp_comments LIMIT 18");
        if ( $comments ) : foreach ($comments as $comment) :
                $post_in[] = $comment->post_ID;
        endforeach; endif;
        echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 21/68
        $my_query = new WP_Query(array('post__in' => $post_in));
        echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 27/88
        if ( $comments ) : foreach ($comments as $comment) :
                $title = get_the_title($comment->post_ID);
                $perma = get_permalink($comment->post_ID);
        endforeach; endif;
        echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 35/96
}

is producing the following $wpdb->queries dump. WordPress wizards, I
need help figuring out why 8 of the posts weren't cached. They are all
ordinary, published posts:

   [30] => Array
        (
            [0] =>  SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM
wp_posts  WHERE 1=1  AND wp_posts.ID IN
(1704,1714,1724,1705,1730,470,67,1016,1714,1720,1701,1640,988,1727,1718,1726,1724,1704)
AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC
LIMIT 0, 6
            [1] => 0.00011420249939
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts
        )

    [31] => Array
        (
            [0] => SELECT FOUND_ROWS()
            [1] => 6.00814819336E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts
        )

    [32] => Array
        (
            [0] => SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN
wp_term_relationships AS tr ON tr.term_taxonomy_id =
tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag') AND
tr.object_id IN (1730, 1714, 1727, 1726, 1724, 1704) ORDER BY t.name
ASC
            [1] => 5.00679016113E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts,
update_post_caches, update_object_term_cache, wp_get_object_terms
        )

    [33] => Array
        (
            [0] => SELECT post_id, meta_key, meta_value FROM
wp_postmeta WHERE post_id IN (1730,1714,1727,1726,1724,1704)
            [1] => 3.91006469727E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts,
update_post_caches, update_postmeta_cache
        )

    [34] => Array
        (
            [0] => SELECT * FROM wp_posts WHERE ID = 1705 LIMIT 1
            [1] => 5.00679016113E-5
            [2] => require, require_once, include, my_test_function,
get_the_title, get_post
        )

    [35] => Array
        (
            [0] => SELECT * FROM wp_posts WHERE ID = 67 LIMIT 1
            [1] => 5.50746917725E-5
            [2] => require, require_once, include, my_test_function,
get_the_title, get_post
        )

followed by six others in the form of "SELECT * FROM wp_posts WHERE ID
= x LIMIT 1"

2009/10/2 Otto <otto@...>:

> You shouldn't need to call update_post_caches, as the
> WP_Query->query() function does that automatically anyway. query.php,
> line 2359 (or thereabouts).
>
> As for the rest, I don't know. The get_post function should indeed
> check the cache first.
>
> Call global $wp_object_cache; $wp_object_cache->stats(); to see the
> contents of the cache before and after each call. Might help you with
> debugging.
>
> -Otto
> Sent from Memphis, TN, United States
>
>
> On Thu, Oct 1, 2009 at 11:47 AM, William Canino
> <william.canino@...> wrote:
>> I'm coding a Yet-Another-Clickable-List-of-Posts-With-Something-in-Common.
>>  The amount of SQL queries my current code is making is driving me
>> nuts.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by Otto-19 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's fine, but it still doesn't show the actual contents of the
cache. You're going to have to dump the cache before and after the
get_the_title call to see why it's missing.

-Otto
Sent from Memphis, TN, United States
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by William Canino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

Regarding

> $my_query = new WP_Query(array('post__in' => array([very long array of post IDs])));
> update_post_caches($my_query->posts);
> echo $get_permalink(1);
> echo $get_the_title(2);
>
> Do you know why WordPress is still making SQL
> queries on lines 3 and 4? Shouldn't they be already in
> the object cache?

I found the answer ... and subsequently found an irregularity in the
WordPress core.  I don't know if I should call it a bug.

get_posts is automatically adding "LIMIT 0, 6" to the SQL statement. 6
is the number of posts per page set in the blog's Reading settings.
That's why posts 1 and 2 are not in the object cache.  Furthermore,
adding either of the following

'numberposts' => -1  // as per Codex
'nopaging' => true // as per Codex

to the call, such as, $my_query = new WP_Query(array('post__in' =>
[very long array of post IDs], 'nopaging' => true)); makes no
difference.

I opened WordPress's query.php and examined it, and found what I had to use:

new WP_Query(array('post__in' => [very long array of post IDs],
'posts_per_page' => 100000));

or some high number.  This is the solution.

Moral of the Story: If one uses WP_Query for one's ends, he must set
all the parameters he needs so that the custom WP_Query doesn't use
the blog's settings.

Is this an irregularity or is this a bug?

W

2009/10/2 Otto <otto@...>:

> That's fine, but it still doesn't show the actual contents of the
> cache. You're going to have to dump the cache before and after the
> get_the_title call to see why it's missing.
>
> -Otto
> Sent from Memphis, TN, United States
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by Otto-19 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"numberposts" only works with the get_posts call, not with a WP_Query object.

A WP_Query expects either "posts_per_page" or "showposts" (which is
deprecated) for the number of posts.

Setting posts_per_page to -1 will have the same effect as setting
nopaging to true. Either will eliminate the LIMIT section entirely,
making it return all posts.

And yes, if you fail to specify a parameter, then it'll take it from defaults.

-Otto



On Sun, Oct 4, 2009 at 1:04 PM, William Canino
<william.canino@...> wrote:

> Hello all,
>
> Regarding
>
>> $my_query = new WP_Query(array('post__in' => array([very long array of post IDs])));
>> update_post_caches($my_query->posts);
>> echo $get_permalink(1);
>> echo $get_the_title(2);
>>
>> Do you know why WordPress is still making SQL
>> queries on lines 3 and 4? Shouldn't they be already in
>> the object cache?
>
> I found the answer ... and subsequently found an irregularity in the
> WordPress core.  I don't know if I should call it a bug.
>
> get_posts is automatically adding "LIMIT 0, 6" to the SQL statement. 6
> is the number of posts per page set in the blog's Reading settings.
> That's why posts 1 and 2 are not in the object cache.  Furthermore,
> adding either of the following
>
> 'numberposts' => -1  // as per Codex
> 'nopaging' => true // as per Codex
>
> to the call, such as, $my_query = new WP_Query(array('post__in' =>
> [very long array of post IDs], 'nopaging' => true)); makes no
> difference.
>
> I opened WordPress's query.php and examined it, and found what I had to use:
>
> new WP_Query(array('post__in' => [very long array of post IDs],
> 'posts_per_page' => 100000));
>
> or some high number.  This is the solution.
>
> Moral of the Story: If one uses WP_Query for one's ends, he must set
> all the parameters he needs so that the custom WP_Query doesn't use
> the blog's settings.
>
> Is this an irregularity or is this a bug?
>
> W
>
> 2009/10/2 Otto <otto@...>:
>> That's fine, but it still doesn't show the actual contents of the
>> cache. You're going to have to dump the cache before and after the
>> get_the_title call to see why it's missing.
>>
>> -Otto
>> Sent from Memphis, TN, United States
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers@...
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: get_post() not fetching from post cache after update_post_caches()

by William Canino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Otto, I use posts_per_page now. ...

... only to realize that running a dozen get_permalinks(ID) is still
faster than running one WP_Query(array('post__in' => array([a dozen
post IDs])));.

I ended up coding for two cases. If the post IDs are over a dozen, I
run a WP_Query first.

I hope this tip helps another.


2009/10/5 Otto <otto@...>:

> "numberposts" only works with the get_posts call, not with a WP_Query object.
>
> A WP_Query expects either "posts_per_page" or "showposts" (which is
> deprecated) for the number of posts.
>
> Setting posts_per_page to -1 will have the same effect as setting
> nopaging to true. Either will eliminate the LIMIT section entirely,
> making it return all posts.
>
> And yes, if you fail to specify a parameter, then it'll take it from defaults.
>
> -Otto
>
>
>
> On Sun, Oct 4, 2009 at 1:04 PM, William Canino
> <william.canino@...> wrote:
>> Hello all,
>>
>> Regarding
>>
>>> $my_query = new WP_Query(array('post__in' => array([very long array of post IDs])));
>>> update_post_caches($my_query->posts);
>>> echo $get_permalink(1);
>>> echo $get_the_title(2);
>>>
>>> Do you know why WordPress is still making SQL
>>> queries on lines 3 and 4? Shouldn't they be already in
>>> the object cache?
>>
>> I found the answer ... and subsequently found an irregularity in the
>> WordPress core.  I don't know if I should call it a bug.
>>
>> get_posts is automatically adding "LIMIT 0, 6" to the SQL statement. 6
>> is the number of posts per page set in the blog's Reading settings.
>> That's why posts 1 and 2 are not in the object cache.  Furthermore,
>> adding either of the following
>>
>> 'numberposts' => -1  // as per Codex
>> 'nopaging' => true // as per Codex
>>
>> to the call, such as, $my_query = new WP_Query(array('post__in' =>
>> [very long array of post IDs], 'nopaging' => true)); makes no
>> difference.
>>
>> I opened WordPress's query.php and examined it, and found what I had to use:
>>
>> new WP_Query(array('post__in' => [very long array of post IDs],
>> 'posts_per_page' => 100000));
>>
>> or some high number.  This is the solution.
>>
>> Moral of the Story: If one uses WP_Query for one's ends, he must set
>> all the parameters he needs so that the custom WP_Query doesn't use
>> the blog's settings.
>>
>> Is this an irregularity or is this a bug?
>>
>> W
>>
>> 2009/10/2 Otto <otto@...>:
>>> That's fine, but it still doesn't show the actual contents of the
>>> cache. You're going to have to dump the cache before and after the
>>> get_the_title call to see why it's missing.
>>>
>>> -Otto
>>> Sent from Memphis, TN, United States
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers@...
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers@...
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers