Opened 3 years ago

Last modified 3 years ago

#13989 new enhancement

Recent Comments widget optimization

Reported by: dd32 Owned by:
Priority: normal Milestone: Future Release
Component: Optimization Version: 3.0
Severity: normal Keywords: needs-patch
Cc:

Description

Currently when permalinks are enabled, the recent comments widget will call get_post() on each parent post to retrieve its permalink.

In the event that the posts the recent comments are on are not in the current query (for example, posts on old posts, or if we're currently on a singular view), then get_post() will perform a SQL query to load the postdata.

The attached patch introduces a cache_posts($post_ids, $term_cache, $postmeta_cache); function to mass load/cache a set of posts through the usage of WP_Query

The function is smart enough not to query for posts which are already in the Object cache (ie. If the comments are on the current page, it'll skip, or if its in a memory cache)

An example of the SQL query generated by this query:

SELECT wp_posts . * 
FROM wp_posts
WHERE 1 =1
AND wp_posts.ID IN ( 95, 98, 106 ) 
AND wp_posts.post_type IN ('post', 'page', 'attachment', 'wiki', 'note', 'odd')
AND (wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')

Explain'd:

id 	select_type 	table 	 type 	possible_keys 			key 	key_len 	ref 	rows 	Extra 
1	SIMPLE		wp_posts range	PRIMARY,type_status_date	PRIMARY	8		NULL	3	Using where

Attachments (1)

13989.diff (2.3 KB) - added by dd32 3 years ago.

Download all attachments as: .zip

Change History (3)

dd323 years ago

comment:1   dd323 years ago

Can anyone see any issues with that attack for caching these posts?

This doesn't just apply to comments, it could be used for anything which is going to call get_post() multiple times.

IMO, It slots in beside update_object_term_cache() / update_postmeta_cache() nicely.

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Triage to Future Release

In menus, we used a straight get_posts call:

get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
Note: See TracTickets for help on using tickets.