Opened 15 years ago
Closed 9 years ago
#13989 closed enhancement (fixed)
Recent Comments widget optimization
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 3.5 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Widgets | Keywords: | needs-patch dev-feedback |
Focuses: | 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)
Change History (6)
#2
follow-up:
↓ 3
@
14 years ago
- 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) );
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.